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!
you need to convert your leaderstats to a dictionary and then use datastore to save the dictionary.
there
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
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
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.
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
Hello, I don’t understand why I need “PlayerKey”. Can you explain it more? Thanks.
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:
-
1- Go To Game Settings.
-
2- Go To Security. Note: You Must Publish Game To Access The Settings.
-
2- Enable API Service And Click Save.
Great Job! You are Done Step 1 Successful!
2. Coding The Script
- 1- Make Script In ServerScriptService.
- 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:
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!