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

How can I fix my money data save?

I found a data save from the Roblox wiki and it works well except if you go in game with some ingame money and spend it to 0, then if you leave and rejoin the game, you will have your original amount of money. It doesn't seem to save 0s.

scoreKey = "PlayerScore"
 
game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then
	    -- create leaderboard
	    local ls = Instance.new("IntValue")
	    ls.Name = "leaderstats"
	    ls.Parent = player
 
	    --create the score stat
	    local score = Instance.new("IntValue")
	    score.Name = "Money"
	    score.Parent = ls
        score.Value = player:LoadNumber(scoreKey)
    end
end)
 
game.Players.PlayerRemoving:connect(function(player)
    if player:FindFirstChild("leaderstats") then
        player:SaveNumber(scoreKey, player.leaderstats.Money.Value)    
    end
end)

Please help, I assume is is only a matter of a few lines of code.

1 comment

0

M39a9am3R - When I put it in Roblox, it says it is broken and the last end is underlined.

2 answers

✓ Accepted answer

I've had this problem in the past, all you will need to do is make the 0 into a -1, you're correct just a few lines of code.

scoreKey = "PlayerScore"

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then
        -- create leaderboard
        local ls = Instance.new("IntValue")
        ls.Name = "leaderstats"
        ls.Parent = player

        --create the score stat
        local score = Instance.new("IntValue")
        score.Name = "Money"
        score.Parent = ls
        if player:LoadNumber(scoreKey) >= -1 then --If score is less than 0 then
          score.Value = 0 --Score will equal 0
        else --otherwise
          score.Value = player:LoadNumber(scoreKey) --It will equal the saved number.
       end
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    if player:FindFirstChild("leaderstats") then
        if player.leaderstats.Money.Value == 0  then --If Money value equals 0 then
            player:SaveNumber(scoreKey, -1) --Save the money as -1
        else --Otherwise
            player:SaveNumber(scoreKey, player.leaderstats.Money.Value)   --Save it as you have it.
    end
end)

Hope this helps.

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