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

Leaderstats wont save given money to player?

So whenever i give myself money for testing as in:

game.Players.LocalPlayer.Leaderboard.Money.Value = game.Players.LocalPlayer.Leaderboard.Money.Value + 222

it wont save. It also does not save if i do this via a script. Maybe there is some way via server side giving but i have no idea.

The leaderboard + saving and loading script:

local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderboard = Instance.new("Folder", Player)
	Leaderboard.Name = "Leaderboard"
	
	local Money = Instance.new("IntValue", Leaderboard)
	Money.Name = "Money"
	
	local Data = MoneyDataStore:GetAsync(Player.UserId)
	if Data then
		Money.Value = Data["Money"]
	end	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	MoneyDataStore:SetAsync(Player.UserId, {
		["Money"] = Player.Leaderboard.Money.Value;
	})
end)

1 answer

✓ Accepted answer

There are a lot of suggestions I have with this.

First of all, if you're running this in studio, turn on API services. Datastores cannot run in studio with this turned off.

Secondly, Roblox Studio is weird with datastores and sometimes they don't save in studio because the PlayerRemoving function doesn't work.

Third, you should use pcalls. If an error happens while saving or loading, pcalls are a handy way to fix it and find out what the error is. You can learn more about pcalls here: https://devforum.roblox.com/t/pcalls-when-and-how-to-use-them/393687

Fourth, if you do want to run it in studio, use a BindToClose. I know a lot of people are against this, but in my experience, it has always worked. A BindToClose basically gives the server more time to save data while it's shutting down. Even if you don't want to run it in studio, BindToClose is still something to think about. You can learn more about BindToClose here: https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

Just one more note: If it doesn't work in studio, always try in a real game. Studio is very finnicky with datastores, and from my experience, it's better to just test in the real game.

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