Important modules (called functions by some) you need to know:
Player:WaitForDataReady()
pcall(function() end)
Player:SaveBoolean(String key, Boolean value)
Player:SaveString(String key, String value)
Player:SaveNumber(String key, Number value)
Player:SaveInstace(String key, Instance value)
Player:LoadBoolean(String key)
Player:LoadString(String key)
Player:LoadNumber(String key)
Player:LoadInstace(String key)
What's all this about LoadInstances, WaitForDataReady, pcalls? Okay, I'll try to explain it. I'll start with the basics. A Player can recieve data that can be recalled later (even on a different server, although not on a different game) using a Load. To save data, first you need to get to the Player you want to save to. Then you use the appropriate Save function to save your data. Then replace String key with the name under which the data will be saved. Remember, this is a string value, so it needs "" even if its a name made up of numbers. Next, replace the other value with the type of data your saving. Let's say you're trying to save a true or false statement under the name "Bool". This is how it should look:
- Code:
-
player = game.Players:FindFirstChild("m27frogy")
player:SaveBoolean("Bool", true)
Technically speaking, this previous code would not work, so don't try. First, when the game first starts, "player" won't be ready to save data yet. That's where WaitForDataReady comes in. This waits until the player is ready before it continues the script. Usually soon after a player gets into the game, he's ready to recieve data. So here's our new script:
- Code:
-
player = game.Players:FindFirstChild("m27frogy")
player:WaitForDataReady()
player:SaveBoolean("Bool", true)
So is the script ready yet? Not quite. There's still a problem. What if for some reason, the Save didn't work? The script would crash, right? What we need is a way to decide if the code executed or not, then display a error message if it didn't... The answer is pcall! pcall is a protected call, it's also the only way a Save or Load will run. You could just say pcall(function() player:SaveBoolean("Bool", true) end), but that isn't very effective. It still has no way of telling the user a problem occured until it's too late. Why not use a if not pcall() then end? This says basically, if I can't do this pcall, then do this. Using this knowledge, I'd use this script:
- Code:
-
player = game.Players:FindFirstChild("m27frogy")
player:WaitForDataReady()
if not pcall(function() player:SaveBoolean("Bool", true) end) then
print("Oopsy, the save made a mistake.")
end
Great, the script works now, right? Wait a moment, there's no way of retrieving the data! Load works the same as save except you need to know only the string name and it returns something. The best way to use it is to declare it as a variable. The final code will show you what I mean:
- Code:
-
player = game.Players:FindFirstChild("m27frogy")
player:WaitForDataReady()
if not pcall(function() player:SaveBoolean("Bool", true) end) then
print("Oopsy, the save made a mistake.")
end
if not pcall(function() bool = player:LoadBoolean("Bool") end) then
bool = false
print("The 'Bool' value doesn't exist! Defaulting!")
end
print(bool)
For Advanced Scripters:
I've used these techniques to build a saving text editor. I hope ROBLOX's next invention will be saving to the game, not just to players.