“Datastores” You’ve probably heard of them before. Datastores are basically places where you store data even when you’re out of the game. Datastores are all over the place in games used for currency systems and points on the leaderboard.
In this tutorial, you will learn how to:
- Create and use a datastore.
- Briefly learn how to create global leaderstats
First of all, you need to make a script and put it in ServerScriptStorage like so.
Next, you need to get your datastore by doing what is shown below in the script. This basically makes a variable called, “datastore” that represents the datastore service and a variable called, “cash” that represents your data store called, “Cash”.
local datastore = game:GetService("DataStoreService")
local cash = datastore:GetDataStore("Cash")
In this tutorial, I will be showing you how to make leaderstats save in a datastore so here is how you make leaderstats.
First, you have to create a folder called, “leaderstats” by typing in:
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
end)
This is creating a variable called leaderstats representing a folder that has the name, “leaderstats” and is under player.
After you do this, you should make another variable called, “Cash” for the cash leaderstat. It should be an IntValue, have the name, “Cash”, and have the parent, “leaderstats” like shown below.
We’re not going to assign the value yet because we are going to get that data from the datastore.
Note that there are 2 different variables which are, “cash” which represents the datastore and “Cash” which is the actual leaderstat.
Now we are going to get the datastore value by using a protected call function (pcall) to get the value from the datastore and saving it to a variable by doing:
local data
local success,err = pcall(function()
data = cash:GetAsync(plr.UserId.."-Cash")
end)
end)
This saves whether or not there was an error saving the datastore value to the data variable. If there was, success will be false and the variable “err” will hold the error message. Otherwise, success will be false and data will have a value of nil.
Next, we will make make a warn message if the data wasn’t recieved by doing this:
This checks whether or not success is equal to true. If it is, then it will set the cash leaderstat to whatever the data from the datastore was. If success is equal to false then it will warn that there was an error and it will give that error message.
Now, we will make a function to save the data whenever a player leaves the game. First of all, you need to put in the function to see when a player leaves as shown below.
Now we need to put in the function to set the datastore. It’s almost the same thing as before but slightly different. Instead, we don’t need the data variable and we need to specify what we want to save like shown below.
The first parameter for SetAsync is the key and the second one is what you want to save which is the value of cash.
Finally, you should make a message that says whether or not the data save was successful or not by doing the same thing as before.