How can I make a datastore for my leaderstats?

Hello, I want to make a datastore for my leaderstats.
I found a tutorial to create datastore by entering the player’s userId, but I thought that I would have to enter every player’s data.
I want to create a datastore script that saves automatically, and for all the people who join my game.
I would like some examples of the code. Thank you!

5 Likes

you need to convert your leaderstats to a dictionary and then use datastore to save the dictionary.

3 Likes

there

3 Likes

So do I need to make it like this?

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
 
local success, err = pcall(function()
	-- My leaderstats script
end)
 
if success then
	print("Success!")
end
3 Likes

Heres a very simple example of using data stores with leaderstats

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

local data = {
     coins = 5
}
 
game.Players.PlayerAdded:Connect(function(Player)
     local GetDataSuccess, GetDataErr = pcall(function() -- pcall is to dectect errors and handle them
          local datastore = experienceStore:GetAsync(PlayerKeyHere) -- replace PlayerKeyHere with something like Player.Id to define the datastore key that the player is using
     end)

     if not GetDataSuccess then
          Player:Kick(GetDataErr.." (Rejoin please!)") -- Kick player if datastore service isnt working
     end)

     local leaderstats = Instance.new("Folder", Player)
     local Coins = Instance.new("IntValue", leaderstats)

     Coins.Name = "Coins"

     if datastore == nil then
          leaderstat_intvalue = data.Coins --If players first time joining fill in values with defaults
     end
end)

local function saveData(PlayerKey, Player) --Player key would be PlayerKeyHere seen in the earlier pcall function
          local SaveDataSuccess, SaveDataErr = pcall(function() 
                experienceStore:UpdateAsync(PlayerKey, Player.leaderstats.Coins.Value)
          end)
end)

Edit: The comments are kinda confusing so ill be happy to explain further
Another edit: Once you get used to this concept (If you aren’t already) I would highly recommend using ProfileService or DataStore2

8 Likes

local datastore = experienceStore:GetAsync(PlayerKeyHere) – replace PlayerKeyHere with something like Player.Id to define the datastore key that the player is using

Do I need to replace with a specific player Id?
Thanks. :grinning:

1 Like

Player.Id would be the key for the player thats joining and leaving, so if I joined the datastore key used for me would be 1637445410 because thats my User Id

EDIT: I REALIZED THAT IV BEEN SAYING Player.Id INSTED OF Player.UserId, SORRY

2 Likes

Hello, I don’t understand why I need “PlayerKey”. Can you explain it more? Thanks. :grinning:

1 Like

Hi There! I am @AmitRoblox1987. I Be Happy To Help You With DataStores.

What Is DataStores?

Data stores are a storage feature for Roblox games. They can be used to save data which should persist between game sessions, including items in a player’s inventory, experience points, or almost anything else.

How To Make DataStore?

Here Are The Steps:

1. Enable API Service

For Let Your Game To Access The DataStore Service, You Have To Enable API Service.

Here Is How:

Great Job! You are Done Step 1 Successful!

2. Coding The Script
  • 1- Make Script In ServerScriptService.

Shot 0004

  • 2- Start Coding The Script.

Done! Your LeaderStats Are Ready!

Note: Script Made By @ScriptedWyatt

If You Need More Help, Make Sure Visit His Channel!

Channel Link:

https://www.youtube.com/channel/UCpJKKVwMJbjuUX36K16hMJQ

Also The Answer Of Your Question: We Need PlayerKey(Player UserId) To Show DataStore Which Player With UserId Must Have Their Data. For Example Player With UserId 123456789 Must-Have 400 Coins. Player UserId Helps DataStore To Find Player. Then When DataStore Founded Player, Load The Player Data.

I Hope This Help You! Feel Free To Make Another Post. Have a Nice Day/Night! :star2:

10 Likes