Data Persistence Complete Guide: Difference between revisions
>MrNicNac Please do not state who wrote the article or its progress. Just work on it slowly and gradually. |
>61352151511 No edit summary |
||
Line 256: | Line 256: | ||
</pre> | </pre> | ||
The other | == Save Boolean == | ||
Save Boolean can be used to check if a player has visited the game before. You don't need an actual bool object to save. Let's get started with an event that fires when a player leaves the game. | |||
<pre> | |||
game.Players.PlayerAdded:connect(function(p) | |||
</pre> | |||
Now we have p defining the player who left. Now we must wait for their data to be ready. | |||
<pre> | |||
game.Players.PlayerAdded:connect(function(p) | |||
p:WaitForDataReady() | |||
</pre> | |||
Now it's just commen knowledge. In order for them to leave the game they must have entered. So we can assume they have played. We can save a boolean with the value of true to show that they have played. | |||
<pre> | |||
game.Players.PlayerAdded:connect(function(p) | |||
p:WaitForDataReady() | |||
p:SaveBoolean("Played", true) | |||
</pre> | |||
Now the only thing we need is our end | |||
<pre> | |||
game.Players.PlayerAdded:connect(function(p) | |||
p:WaitForDataReady() | |||
p:SaveBoolean("Played", true) | |||
end) | |||
</pre> | |||
The other 3 methods are being written soon. Anything you think can be improved, message 61352151511 on ROBLOX. | |||
== Limitations == | == Limitations == |
Revision as of 18:34, 9 July 2011
Introduction
Data Persistence Saves player's progress in a game. This is a complete tutorial on how it works. There are 8 different methods on how to use it.
Save Number
This first part is a tutorial on how to save a number. In an example we will be saving the players Level. The first part we need is an event to fire when the player is leaving the game.
game.Players.PlayerRemoving:connect(function(p)
There now "p" defines the player that is leaving. Now just in-case they just entered and lost connection, we don't want it to error and we need to wait for them to connect to ROBLOX data-sharing.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady()
There. You now have an event that fires when a player leaves, and waits for their data to load. Now the next thing we want to do is find their leaderstats.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats")
Now that "ls" is searching for leaderstats, we need to make sure it exists.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats") if ls ~= nil then
The "if ls ~= nil then" Checks to see if the leaderstats is there. Now we can move on to finding the Level value.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats") if ls ~= nil then lev = ls:FindFirstChild("Level")
Now lev is finding the level, as with the leaderstats, we need to make sure it exists.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats") if ls ~= nil then lev = ls:FindFirstChild("Level") if lev ~= nil then
Just like "ls", "lev" checks to see if it exists. Now if it does exist, we save its value. To do this you use the SaveNumber method.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats") if ls ~= nil then lev = ls:FindFirstChild("Level") if lev ~= nil then p:SaveNumber("Level", lev.Value)
There, you have just saved a players level. But wait we can't forget our ends, those are the most important thing. So here is our finished script.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() ls = p:FindFirstChild("leaderstats") if ls ~= nil then lev = ls:FindFirstChild("Level") if lev ~= nil then p:SaveNumber("Level", lev.Value) end end end)
Load Number
In the LoadNumber method, you can load a number that a player previously saved. If there was no number saved then the LoadNumber will be 0. Our first line to do this will be for when the player enters the game.
game.Players.PlayerAdded:connect(function(p)
Now p defines the player that was added to the game. Before we can do anything with a players stats, we need to wait until their data is loaded.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady()
Now that the player has their data loaded, we should create their leaderstats.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local ls = Instance.new("IntValue", p) ls.Name = "leaderstats" local lev = Instance.new("IntValue", ls) lev.Name = "Level"
Now we have the leaderstats created. But wait! We don't know what to set the value of "lev" to. So this is where we have to use the LoadNumber method to find out what was saved.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local ls = Instance.new("IntValue", p) ls.Name = "leaderstats" local lev = Instance.new("IntValue", ls) lev.Name = "Level" SavedLevel = p:LoadNumber("Level")
Now SavedLevel is the value that was previously saved. But if a player never visited before, the value would be 0. You can't be level 0 now can you? This is where we have to check to make sure its not 0
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local ls = Instance.new("IntValue", p) ls.Name = "leaderstats" local lev = Instance.new("IntValue", ls) lev.Name = "Level" SavedLevel = p:LoadNumber("Level") if SavedLevel == 0 then SavedLevel = 1 end
Now we are sure they will not be Level 0. Now we just have to set the value of the level.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local ls = Instance.new("IntValue", p) ls.Name = "leaderstats" local lev = Instance.new("IntValue", ls) lev.Name = "Level" SavedLevel = p:LoadNumber("Level") if SavedLevel == 0 then SavedLevel = 1 end lev.Value = SavedLevel
We are almost finished! We just have to add in our ends.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local ls = Instance.new("IntValue", p) ls.Name = "leaderstats" local lev = Instance.new("IntValue", ls) lev.Name = "Level" SavedLevel = p:LoadNumber("Level") if SavedLevel == 0 then SavedLevel = 1 end lev.Value = SavedLevel end)
And we now have a Save/Load level script.
Save String
Using SaveString is alot like SaveNumber. This can be useful for saving player's chat's. I don't know what the point of that would be, maybe to get a few in-game mods to look through everyone's chat's every so often. Well let's get on to the tutorial then. The first thing we need to do is make an event fire when the player leaves the game.
game.Players.PlayerRemoving:connect(function(p)
Now we have to wait for the player's data to load. Just in-case it wasn't loaded already, we don't want to break the script.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady()
Now that we are sure that the data is ready to save/load. We have to find the item we want to save. For now let's just say there is an item called "Chats" in the player.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() c = p:FindFirstChild("Chats")
Now "c" is searching for the StringValue called Chats. We next have to make sure it exists.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() c = p:FindFirstChild("Chats") if c ~= nil then
Now that line is checking to make sure it exists. Using the SaveString method we can save it's value.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() c = p:FindFirstChild("Chats") if c ~= nil then p:SaveString("Chats", c.Value)
Now we are almost done, as always when we are finishing we must add in our ends.
game.Players.PlayerRemoving:connect(function(p) p:WaitForDataReady() c = p:FindFirstChild("Chats") if c ~= nil then p:SaveString("Chats", c.Value) end end)
Load String
In this tutorial we will be using the LoadString method. It will also have a script included to add chat's to a string value whenever somebody says something. We first have to start out with an event that fires when a player enter's the game.
game.Players.PlayerAdded:connect(function(p)
Now "p" is defining the player who joined. As always we need to wait for their data to load before we can do anything with it.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady()
Now the data is ready. The next thing we should do is create the StringValue in the player called "Chats"
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats"
Now we have the StringValue inside of the player. We now have to use the LoadString method to know what to set the value as.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats" C = p:LoadString("Chats")
Now that "c" is the loaded string value, we can set the value of chats.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats" C = p:LoadString("Chats") SV.Value = C
You have just loaded the value. Now I will add in the script to update the value whenever the user talks. The first thing we need is the .Chatted event
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats" C = p:LoadString("Chats") SV.Value = C p.Chatted:connect(function(msg)
"msg" is defining what the thing said was. Now we just need to simply update the value.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats" C = p:LoadString("Chats") SV.Value = C p.Chatted:connect(function(msg) SV.Value = SV.Value.."; "..msg
Now we are almost done. We just need to add in our ends.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() local SV = Instance.new("StringValue", p) SV.Name = "Chats" C = p:LoadString("Chats") SV.Value = C p.Chatted:connect(function(msg) SV.Value = SV.Value.."; "..msg end) end)
Save Boolean
Save Boolean can be used to check if a player has visited the game before. You don't need an actual bool object to save. Let's get started with an event that fires when a player leaves the game.
game.Players.PlayerAdded:connect(function(p)
Now we have p defining the player who left. Now we must wait for their data to be ready.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady()
Now it's just commen knowledge. In order for them to leave the game they must have entered. So we can assume they have played. We can save a boolean with the value of true to show that they have played.
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() p:SaveBoolean("Played", true)
Now the only thing we need is our end
game.Players.PlayerAdded:connect(function(p) p:WaitForDataReady() p:SaveBoolean("Played", true) end)
The other 3 methods are being written soon. Anything you think can be improved, message 61352151511 on ROBLOX.
Limitations
- Data Persistence only works in ROBLOX game Servers. It does not work in Build or Edit mode.
- A maximum of 45000 data complexity units can be saved for each player in 1 game.
- Use: print(Item.DataCost) using the command bar to see the amount of data complexity units it will cost.
- If you try to use DataCost in a normal or local script, it will error.