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

Why aren't my leaderstats saving?

Asked by 3 years ago
Edited 3 years ago

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!

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. deeskaalstickman649 475 — 3y
0
Thanks for that deeskaalstickman! :) PadmeOragana 78 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

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.

0
Ooooh... But why did it save in my other game then? Since this is, like you said, the only script. PadmeOragana 78 — 3y
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.. deeskaalstickman649 475 — 3y
0
What's the load data script? Also, the API requests and HTTP services are on. The game is also published. PadmeOragana 78 — 3y
0
I meant a save script, it's when the script saves the players data whenever one leaves. deeskaalstickman649 475 — 3y
View all comments (3 more)
0
What should I do then? PadmeOragana 78 — 3y
0
Make the script save when a player leaves. deeskaalstickman649 475 — 3y
0
Thanks, but that doesn't work either... I just added that part but it still doesn't save... PadmeOragana 78 — 3y
Ad

Answer this question