Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why aren't my leaderstats saving?

So, for my game, I want my leaderstats to save. I've tried datastores and whatnot, but nothing ever works. I then resorted to using a plain leaderstats save. I reused my old leaderstat save from another one of my old games, and it worked in that game, but it doesn't work for this new game... It's a normal script in serverscriptservice. Code:

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Doubloons"
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 100
	end
	
	cash.Changed:connect(function(Val)
		CashStore:SetAsync("Points_"..player.Name, Val)
	end)

game.Players.PlayerRemoving:Connect(function(player)
    for i,v in pairs(script:GetChildren()) do 
        print("Getting")
        local DataStore = game:GetService("DataStoreService")
        local d = DataStore:GetDataStore(v.Name)
        d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
        print("Saved")
    end
end

game.Players.PlayerAdded:connect(PlayerEntered)

Thank you!

2 comments

0

You should change the 'Points_..player.Name to player.UserId, because the player can change their username, and lose their data. The UserId never changes.

0

Thanks for that deeskaalstickman! :)

1 answer

If this is the only script you have, then the player data only gets loaded, however, never gets saved.

Therefore, the DataStore will hold nothing.

7 comments

0

Ooooh... But why did it save in my other game then? Since this is, like you said, the only script.

0

It's probably because you also added a load data script. Also, make sure studio has access to API requests, HTTP services, and make sure your game is published..

0

What's the load data script? Also, the API requests and HTTP services are on. The game is also published.

0

I meant a save script, it's when the script saves the players data whenever one leaves.

0

What should I do then?

0

Make the script save when a player leaves.

0

Thanks, but that doesn't work either... I just added that part but it still doesn't save...

This discussion is preserved as it was. New answers, comments, and votes are closed.