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

How to save leaderstats?

Asked by 9 years ago

Hello!

I'm making a game, and I want people's KOs to save through its alpha stages.

I've tried to write this code multiple times, and it never works.

I have the basic leaderstats script, except I changed "KOs" and "Wipeouts" to "Kills" and "Deaths".

It's supposed to keep a running value of kills and deaths; if you leave the game at 2 kills, you'll come back with 2, if you leave with 4, next time you come back, you'll have 4.

Here's the saving script:

local KOStore = game:GetService("DataStoreService"):GetDataStore("KO")
local WOStore = game:GetService("DataStoreService"):GetDataStore("WO")

game.Players.PlayerAdded:connect(function(plr)

    plr:WaitForChild("leaderstats")
    plr.leaderstats:WaitForChild("Kills")
    plr.leaderstats:WaitForChild("Deaths")

    local KOkey,WOkey = "user_"..plr.userId.."KO","user_"..plr.userId.."WO"

    plr.leaderstats.Kills.Value,plr.leaderstats.Deaths.Value = KOStore:GetAsync(KOkey),WOStore:GetAsync(WOkey)

end)

game.Players.PlayerRemoving:connect(function(plr)

    local KOkey,WOkey = "user_"..plr.userId.."KO","user_"..plr.userId.."WO"

    KOStore:UpdateAsync(KOkey,function(oldValue)
        if oldValue then
            return (oldValue + plr.leaderstats.Kills.Value)
        else
            return (plr.leaderstats.Kills.Value)
        end
    end)

end)

I've tested this both on studio servers and actual servers, and it doesn't work on either.

0
Anyone? :/ Tortelloni 315 — 9y

1 answer

Log in to vote
1
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

I created a test place and ran your script and it worked, it saved Kills and reloaded them I rejoined. What exactly do you mean by "it doesn't work"?

It doesn't save wipeouts because you forgot to use WOStore:UpdateAsync in your PlayerRemoving function.

The other issue I see is that it's going to basically double your Kills whenever you join a game and your stats have been previously saved. When you join, it sets your Kills Value to the value in the KOStore, and when you leave, it sets it to the oldValue (which is the value in the KOStore) PLUS the current leaderstats.Kills.Value. Instead, you should just set it to the leaderstats.Kills.Value.

0
Thanks so much! I feel stupid for not realizing that WOStore didn't update, but this helped me a lot, so again, thanks! Tortelloni 315 — 9y
Ad

Answer this question