How to use datastores

it's simple :D

by DevFofo

Author Avatar

First, insert a script into the ServerScriptService. (You can get rid of the print("Hello World!"))

Now, we need to detect when a player joins the game. We can do this by typing:

game.Players.PlayerAdded:Connect(function(player)

end)

After we have written that code, we need to define some variables. Let's start by defining the DataStores that we want.

local dss = game:GetService("DatastoreService")
local datastore = dss:GetDatastore("Coins")

In this case we set the datastore to be named "Coins". You can name your datastores whatever you'd like, as long as you keep in mind that you need to be able to remember the names of these datastores if you want to use them again. I recommend keeping the names simple so you won't forget them.

Of course, we want the player to even have a coins instance at all, otherwise there's nothing to save. Let's give the player a coins instance by typing this code into our previous function:

local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"

local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"

Your entire script should now look like this:


local dss = game:GetService("DatastoreService")
local datastore = dss:GetDatastore("Coins")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"

end)

If the player joins the game now, they'll have an instance called "Coins". Now we need to connect our datastore to our coins instance. We can start by making sure that if the player has any saved data, we set that data to the coins instance we just created. We can do this using the following code:

local data = datastore:GetAsync(player.UserId)
if data then
	coins.Value = data
end

So what we just did, we first created a new variable called "data". This variable uses the :GetAsync() function to get the data of the specific key to which that data is connected. The key we used in this code is player.UserId. Every player has a specific UserId. By using this UserId as our key, we can bind our data to only those players with that UserId, and since everyone has a different UserId, we can bind the data to each player individually. After that we created an if-statement to check wether the player already has data or not. If it has data, then coins.Value will be set to that data.

Your script should now look like this:


local dss = game:GetService("DatastoreService")
local datastore = dss:GetDatastore("Coins")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"

	local data = datastore:GetAsync(player.UserId)
	if data then
		coins.Value = data
	end
end)

The next thing we want to do is save the data so that when a player rejoins the game, they keep their data. You can do with two methods. One is saving the data when the player leaves, the other one is saving the players data every so often (like every 1-10 minutes). For this tutorial we'll do the "when the player leaves" method.

To start with this, we need to detect when a player leaves. Very much like the first command, we have to create another one, but now for PlayerRemoving instead of PlayerAdded. The code for that looks like this:

game.Players.PlayerRemoving:Connect(function(player)
	
end)

Now inside this code, we need to save the amount of coins the player has into the datastore. For that, we can use this code:

local coins = player.leaderstats.Coins
datastore:SetAsync(player.UserId,coins.Value)

What we did here was quite simple. First we defined a variable, we defined "coins" to be the coins instance we created earlier. After that, we set the data of the datastore to the value of our coins instance. We did this by first defining a key for our datastore. The key we used has to be the same key as we used in the :GetAsync() function earlier. For practical use the player.UserId would've been the best option anyways. After we defined the key, we need to add a comma so we can define the second instance, the data. In this case, we want the data to be the value of the coins, aka the amount of coins the player has. We can get this by simply typing .Value behind our coins instance, so: coins.Value.

Your script should now look like this:


local dss = game:GetService("DatastoreService")
local datastore = dss:GetDatastore("Coins")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"

	local data = datastore:GetAsync(player.UserId)
	if data then
		coins.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local coins = player.leaderstats.Coins
	datastore:SetAsync(player.UserId,coins.Value)
end)

And that's it! You can test your code now. Just keep in mind that this script is a server-side script. Changing the amount of coins while interacting on the client-side will NOT save your data.

Side note: If you change anything like "Coins" to something else such as "Kills", make sure you rename every single place where you typed "Coins" to "Kills".

View in-game to comment, award, and more!