Hello! I am not good at scripting, in fact I started one week ago that is also why I have no idea why this script is not working.
this script is not made by me I just changed the names of the leaderstats ("Money", "Kills"). When I run the game, I get no errors. it is just not doing anything, whitch makes no sense.
It basically should save the players leaderstats and also should load them when you join the game. If you have a better code please send it to me or correct whats wrong with this one:
local ds = game:GetService("DataStoreService"):GetDataStore("StatusStore0") ns0 = "leaderstats" nc0 = "Money" nc1 = "Kills" sc0 = 0 sc1 = 0 game.Players.PlayerAdded:Connect(function(player) local savedLevel = ds:GetAsync(player.UserId) local stats = Instance.new("Folder") stats.Name = ns0 stats.Parent = player local coins = Instance.new("NumberValue") coins.Name = nc0 coins.Parent = stats local coins1 = Instance.new("NumberValue") coins1.Name = nc1 coins1.Parent = stats if savedLevel then coins.Value = savedLevel[1] coins1.Value = savedLevel[2] else coins.Value = sc0 coins1.Value = sc1 ds:SetAsync(player.UserId,{coins.Value, coins1.Value}) end end) game.Players.PlayerRemoving:Connect(function(player) local stats = player:FindFirstChild(ns0) if not stats then return end local vals = {stats:FindFirstChild(nc0),stats:FindFirstChild(nc1)} if vals and vals[1] and vals[2] then ds:SetAsync(player.UserId,{vals[1].Value, vals[2].Value}) end end)
Here is DataStore script I have that works
local dataStoreService = game:GetService("DataStoreService") local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats") local loaded = {} game.Players.PlayerAdded:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") if player.UserId > 0 and player.Parent then local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId) if leaderstatsData ~= "Request rejected" then if leaderstatsData then for i, Stat in ipairs(leaderstats:GetChildren()) do local value = leaderstatsData[Stat.Name] if value then Stat.Value = value end end end loaded[player] = true end end end) game.Players.PlayerRemoving:connect(function(player) local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then if loaded[player] then local leaderstatsData = {} for i, stat in ipairs(leaderstats:GetChildren()) do leaderstatsData[stat.Name] = stat.Value end leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) end end loaded[player] = nil end)
This is a little hard to explain so basically When a player joins we wait for their leader stats to be created from the script below, then we make sure that the player Userid is loaded by making sure it's above 0, after we load are leader stats and create a loop for each stat replacing the values as needed. Now for saving them we do the same thing by locating their leader stats, then creating a table and inserting each leaderstats into that table, and then sending that table to the dataStore
And as far as your leader stats you do need to name the folder "leaderstats" or else it will not work
Here is an example of the leader starts script I have:
local StatsTable = { "Cash", "Rebirths", "Kills", "KOs" } game.Players.PlayerAdded:Connect(function(p) local leaderstats = Instance.new("Folder", p) leaderstats.Name = "leaderstats" for i = 1, #StatsTable do local stat = Instance.new("IntValue", leaderstats) stat.Name = StatsTable[i] stat.Value = 0 end end)
To explain this script P is Player and all I'm doing is when the player is added I create a folder and make the parent the player, Then I name it accordingly.
In the For loop, it just goes through each Stat I have in the table above and creates a new IntValue and names it, and sets it to value accordingly.
Hopefully, this helps, or at least you can use it to try to see if you can make some sense out of it.