Simple Datastore

Before making this , make sure you are already pretty comfortable with script.

by thanks1234t

Author Avatar

Dont make this if your only starting out , it is a bit advanced. Anyways lets get started!!

NOTE: this is done in a server script.

Step 1:

Most of you will know how to make a leaderstats but ever wondered how to make it save? Lets start off by making our datastore :

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("DataStore")

Very Simple Stuff , the first line gets the service and the second one creates it! Now lets make our leaderstats as normal , if you dont know how then this tutorial might not be for your level.

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("DataStore")

game.Player.PlayerAdded:Connect(function("Player") -- when a player is added it runs

local leaderstats = Instance.new("Folder") -- creates a folder 
leaderstats.Parent = Player -- parents the folder to player
leaderstats.Name = "leaderstats" -- names the folder to leaderstats

local Coins = Instance.new("IntValue")
Coins.Parent = leaderstats          -- Same thing done on this bit
Coins.Name = "Coins

end)

Step 2:

For step 2 we wanna start the saving data so we will do the first bit , start off by making this variable


local data

then we wanna make another variable thats sort of a function.


local data
local success, errormessage = pcall(function())

end)

very simple , now its kinda hard to explain this so lets just work with it , this is one of the bits you just gotta kinda just remember inside the function we will make set data to the value that saved when leaving the game by using this:

local data
local success, errormessage = pcall(function())
data = datastore:GetAsync("Player_"..player.UserId)
end)

easy right? that just gets the data by using the player id now lets check if its a success with a if statement if its a success then we will set the Coins value to data:

local data
local success, errormessage = pcall(function())
data = datastore:GetAsync("Player_"..player.UserId)
end)

if success then
Coins.Value = data
end

Step 3:

thats it for the player joined function , now lets make a function when a player leaves by using this:

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

end)

now first we will make a variable named data , this variable will be set to Coins value , and eventually we will save this data which is the one it will load.

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

local data = player.leaderstats.Coins.Value
end)

there we go , now lets do the success errormessage function again , your almost there..

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

local data = player.leaderstats.Coins.Value

local success, errormessage = pcall(function())

   end)
end)

we will then now save it by instead of doing datastore:GetAsync() we will do datastore:SetAsync() watch this :

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

local data = player.leaderstats.Coins.Value

local success, errormessage = pcall(function())
    datastore:SetAsync("Player_"..player.UserId, data)
   end)
end)

so once again we do it with the player id but then we put data to send it over so when a player joins their Coins will be set to data which is recieved from the player id , sorry if you dont understand it. Next bit , lets check for success!

if success then

end

except all we doing now is just printing since its already saved , but then if its not success then we will warn :

if success then
print("Saved Data")

elseif not success then
warn(errormessage)
  end

thats it for the script , BUT before you run it , make sure to save the game , then go to game settings and enable API Services or else the datastore script wont work!

Your script should look like this:

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("DataStore")

game.Player.PlayerAdded:Connect(function("Player") -- when a player is added it runs

local leaderstats = Instance.new("Folder") -- creates a folder 
leaderstats.Parent = Player -- parents the folder to player
leaderstats.Name = "leaderstats" -- names the folder to leaderstats

local Coins = Instance.new("IntValue")
Coins.Parent = leaderstats          -- Same thing done on this bit
Coins.Name = "Coins

local data
local success, errormessage = pcall(function())
data = datastore:GetAsync("Player_"..player.UserId)
end)

if success then
Coins.Value = data
end

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

local data = player.leaderstats.Coins.Value

local success, errormessage = pcall(function())
    datastore:SetAsync("Player_"..player.UserId, data)
   end)

if success then
print("Saved Data")

elseif not success then
warn(errormessage)
  end

end)

You have successfully made a data store , but if you want to store multiple values then read the next step

But how do I save multiple Values?

simple.First we will make more values


local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("DataStore")

game.Player.PlayerAdded:Connect(function("Player") -- when a player is added it runs

local leaderstats = Instance.new("Folder") -- creates a folder 
leaderstats.Parent = Player -- parents the folder to player
leaderstats.Name = "leaderstats" -- names the folder to leaderstats

local Coins = Instance.new("IntValue")
Coins.Parent = leaderstats          -- Same thing done on this bit
Coins.Name = "Coins

local Wins = Instance.new("IntValue")
Wins.Parent = leaderstats           -- I made wins here!!
Wins.Name = "Wins"

local data
local success, errormessage = pcall(function())
data = datastore:GetAsync("Player_"..player.UserId)
end)

if success then
Coins.Value = data
end

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

local data = player.leaderstats.Coins.Value

local success, errormessage = pcall(function())
    datastore:SetAsync("Player_"..player.UserId, data)
   end)

if success then
print("Saved Data")

elseif not success then
warn(errormessage)
  end

end)

Next we will change this :


local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("DataStore")

game.Player.PlayerAdded:Connect(function("Player") -- when a player is added it runs

local leaderstats = Instance.new("Folder") -- creates a folder 
leaderstats.Parent = Player -- parents the folder to player
leaderstats.Name = "leaderstats" -- names the folder to leaderstats

local Coins = Instance.new("IntValue")
Coins.Parent = leaderstats          -- Same thing done on this bit
Coins.Name = "Coins

local Wins = Instance.new("IntValue")
Wins.Parent = leaderstats           -- I made wins here!!
Wins.Name = "Wins"

local data
local success, errormessage = pcall(function())
data = datastore:GetAsync("Player_"..player.UserId)
end)

if success then
Coins.Value = data.Coins -- I CHANGED HERE!! Data will be a table so we need to look at Coins inside data
Wins.Value = data.Wins  -- I CHANGED HERE!! same here 
end)

Now we will make data a table in the playerremoving function:

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

local data = {

Coins = player.leaderstats.Coins.Value,   -- turned into a table
Wins = player.leaderstats.Wins.Value

}

local success, errormessage = pcall(function())
    datastore:SetAsync("Player_"..player.UserId, data)
   end)

if success then
print("Saved Data")

elseif not success then
warn(errormessage)
  end

end)

Finally..

You've reached the end .. Well Done , if you didnt skip anything in this tutorial then you might have learnt something new , if not im sorry. I did my best on this tutorial so I just want you to be able to make it.

I would like to see your comments so feel free to comment anything!

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