generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How do I use Data Persistence to save a players item(s)?

Previous Thread :: Next Thread 
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
20 Apr 2013 11:30 PM
I am making a fan-made egg hunt, and I'm trying to save what eggs a person would have when they join a server.

Any idea on how to do that?
I know SaveInstance and stuff, but I am not to that level yet, and I have no idea how to use it.

Replies would be appriciated.


-pie
Report Abuse
Xnite515 is not online. Xnite515
Joined: 18 Feb 2011
Total Posts: 22763
20 Apr 2013 11:40 PM
player:SaveInstance("model", game.Workspace.Part)

--


a =player:LoadInstance("model")
a.Parent = workspace
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
20 Apr 2013 11:44 PM
"player" was never defined.

What do I use to get player?
I know of a few ways to define a player, but I'm not sure which is the preferred way.

LocalPlayer
game.Players.ChildAdded:connect(function(player) end)

Stuff like that.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
20 Apr 2013 11:48 PM
I'll give you an example that would only work if the eggs are Tools or HopperBins. If they're not, just improvise using what you learned.

game.Players.PlayerAdded:connect(function(player)
        player:WaitForDataReady()
        player.CharacterAdded:connect(function(char)
                local model = player:LoadInstance("Eggs")
                if model then
                        for k,v in pairs(model:GetChildren()) do
                                v.Parent = player.Backpack
                        end
                end
        end)
end)

game.Players.PlayerRemoving:connect(function(player)
        local eggs = Instance.new("Model", player)
        for k,v in pairs(player.Backpack:GetChildren()) do
                v.Parent = eggs
        end
        for k,v in pairs(player.Character:GetChildren()) do --in case an egg is being selected
                if v:IsA("Tool") then
                        v.Parent = eggs
                end
        end
        player:SaveInstance("Eggs", eggs)
end)

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
20 Apr 2013 11:48 PM
Bump

(I know I'm not good at scripting compared to a lot of people.)
Report Abuse
mister1nothing is not online. mister1nothing
Joined: 27 Dec 2007
Total Posts: 579
20 Apr 2013 11:51 PM
It doesn't matter how you get player, it jus matters that you get player.

My favorite method is with an invisible GUI that sends a script the contents of script.Parent.Parent.Parent.Owner
Report Abuse
Xnite515 is not online. Xnite515
Joined: 18 Feb 2011
Total Posts: 22763
20 Apr 2013 11:53 PM
what mister said
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 12:01 AM
Electric, I used your script (put it in Workspace).

However, I don't understand exactly what it's supposed to do.

And yes, I'm not that good at scripting.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 12:09 AM
Here, I'll comment each line with an explanation for you.

game.Players.PlayerAdded:connect(function(player)
--The following will happen when a player joins.
player:WaitForDataReady()
--Wait until Data Persistence is accessible to the player.
player.CharacterAdded:connect(function(char)
--The following will happen when the player SPAWNS.
local model = player:LoadInstance("Eggs")
--Search for a model saved with the keyword "Eggs"
if model then
--If a model is found, do the following
for k,v in pairs(model:GetChildren()) do
v.Parent = player.Backpack
end
--Move all of the children of that model into the player's Backpack
end
end)
end)

game.Players.PlayerRemoving:connect(function(player)
--The following will happen when a player leaves.
local eggs = Instance.new("Model", player)
--Create a model in the player.
for k,v in pairs(player.Backpack:GetChildren()) do
v.Parent = eggs
end
--Move all of the objects in the player's backpack to that model.
for k,v in pairs(player.Character:GetChildren()) do --in case an egg is being selected
if v:IsA("Tool") then
v.Parent = eggs
end
end
--Find out if there's an egg already being selected, and if there is, move it to the model.
player:SaveInstance("Eggs", eggs)
--Save this model to the player using the keyword "Eggs"
end)

So, when a player leaves, all of its eggs are saved in a model with a keyword "Eggs." When the player re-joins, it checks to see if they have eggs saved. If they do, then the eggs are moved to the player's Backpack.

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 12:12 AM
Okay, thank you.

I picked up a tool that wasn't in the StarterPack (just a brick tool that's named testegg), left and came back, and it wasn't in my toolbar.

Sorry if it feels like I'm leaning on you too much.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 12:49 AM
Fixed it. I tested it. I also added several safe-cautions. Just note that if you're selecting a tool when you leave, it won't be saved, because a player's character is deleted before the PlayerRemoving event fires. I'll let you work that out.

game.Players.PlayerAdded:connect(function(player)
        player.CharacterAdded:connect(function(char)
                player:WaitForDataReady()
                local model = player:LoadInstance("Eggs")
                if model then
                        model.Parent = player
                        for k,v in pairs(model:GetChildren()) do
                                v.Parent = player.Backpack
                        end
                        model:destroy()
                end
        end)
end)

game.Players.PlayerRemoving:connect(function(player)
        local eggs = Instance.new("Model", player)
        for k,v in pairs(player.Backpack:GetChildren()) do
                v.Parent = eggs
        end
        if player.Character ~= nil then
                for k,v in pairs(player.Character:GetChildren()) do
                        if v:IsA("Tool") then
                                v.Parent = eggs
                        end
                end
        end
        player:SaveInstance("Eggs", eggs)
end)

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Maplen is not online. Maplen
Joined: 19 Sep 2012
Total Posts: 545
21 Apr 2013 12:58 AM
Bump
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 07:42 AM
Thank you, it works.

I have tried to convert your script into simply saving the data that you have the egg, instead of having to make all my eggs tools.

However, I see no way on how to do this.
I'm sorry for asking so many questions, this is the only part of my game where I'm going to the scripting helpers forum, because I have no knowledge on Data Persistence.

Thanks for helping me so far,

-pie
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 07:44 AM
That depends on what the eggs _are_. Are they just single parts?

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 07:46 AM
Yes. Bricks with scripts.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 07:47 AM
How do players see the eggs they've touched?

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 07:48 AM
Oh god that came out wrong.... Wow...

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 07:49 AM
Through a GUI.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 07:50 AM
Save the GUI as a model so that I can see its hierarchy.

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 07:57 AM
Haven't created it yet, but it shouldn't take me more than five minutes, I'll do that now.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
21 Apr 2013 07:58 AM
Alright. I have to go right now, so I'll work it out later.

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
21 Apr 2013 02:47 PM
I will post it later today.
Report Abuse
mister1nothing is not online. mister1nothing
Joined: 27 Dec 2007
Total Posts: 579
22 Apr 2013 02:51 PM
There's a bug in Blaze' script.

It doesn't actually load anything, it just waits for the player to have Data Ready.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
23 Apr 2013 01:58 AM
@mister - Yeah, I recommend you go see an eye doctor. :P

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
pieinyofaceha is not online. pieinyofaceha
Joined: 09 May 2012
Total Posts: 2367
23 Apr 2013 06:48 AM
@mister

His works perfectly, I don't know where you're coming from, sorry.

Electric, when you're online, I'll make the model free.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image