Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I fix my money data save?

Asked by 9 years ago

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.

0
M39a9am3R - When I put it in Roblox, it says it is broken and the last end is underlined. Champion121212 22 — 9y

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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.

Ad
Log in to vote
0
Answered by 9 years ago

Thank you so much!

Answer this question