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)

4 comments
So, I just put it in the ServerScriptService and it is still not working. I published the game then I joined and played a bit so my stats are changing. Then I logged off and then logged on again and it saved no stats. What am I making wrong? Should I make the game public first? Because obviously this is my fault.
You do need to make sure you allow the api for your game Via GameSettings < Security < Enable Studio Access to API Services. Along with this you cant chang the value in studio unless its through a script but if you publish the game and then opent the devloper consol and type in "game.Players.YourName.leaderstats.Cash.Value = 100" and rejoin it should save. Lastly they are 2 diffrent scripts
I enabled Studio Access to API Services and I also changed the stats by playing it through the website and then joined the game. I also put those into two diffrent scripts. And it still is not working. It is not making any sense in my eyes that this is not working. I can show you the game if you want.
Okay I think I have found the problem now. As I thought the problem was not becouse you it was becouse I have the thing that updates the leaderstats in a localscript. So others can’t see what stats I have and your script just saved 0.