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

My Leadestats won't save, why not?

Asked by 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. I had to repost this since I wasn't getting any more responses. Code:

local 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.UserID) ~= nil then
        cash.Value = CashStore:GetAsync("Points_"..player.UserID)
    else
        cash.Value = 100
    end

    cash.Changed:connect(function(Val)
        CashStore:SetAsync("Points_"..player.UserID, Val)
    end)
    game.Players.PlayerRemoved:Connect(function()
    CashStore:SetAsync(player.UserID, cash.Value) 
end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

Thank you!

0
Try moving lines 23-25 to the end of the script. User#30567 0 — 3y
0
@TTChaos, Hi, thanks for responding! Unfortunately, that doesn't work. PadmeOragana 78 — 3y
0
Can you send us a screenshot of you testing in studio User#30567 0 — 3y

2 answers

Log in to vote
1
Answered by
I_Nev 200 Moderation Voter
3 years ago

Look into datastore2, its very very good

0
Hi, thanks for replying. What's datastore2? PadmeOragana 78 — 3y
0
A little bit complex, but way more reliable and easy to use version of datastore I_Nev 200 — 3y
0
Thank you, I'll try that! PadmeOragana 78 — 3y
0
Thank you, DataStore2 works!!!! :D PadmeOragana 78 — 3y
Ad
Log in to vote
2
Answered by
Ieowyyn 69
3 years ago

Hello! Have you tried putting using the "PlayerRemoved" event outside of the PlayerAdded event? Also, your keys are different. You used different keys to save when leaving, and when the value changes. If you want to grab a key that looks like this: CashStore:GetAsync("Points_"..player.UserID) Then save a key that looks the same: CashStore:SetAsync("Points_"..player.UserID, cash.Value) If you don't know what I mean, it should look something like this.

local 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.UserID) ~= nil then
        cash.Value = CashStore:GetAsync("Points_"..player.UserID)
    else
        cash.Value = 100
    end

    cash.Changed:connect(function(Val)
        CashStore:SetAsync("Points_"..player.UserID, Val)
    end)
end

function PlayerLeft(player)
    CashStore:SetAsync("Points_"..player.UserID, cash.Value)
end)

game.Players.PlayerRemoved:connect(PlayerLeft)
game.Players.PlayerAdded:connect(PlayerEntered)

Apologies if I made any mistakes, I wrote this with a burnt hand.

0
Accept the answer above User#30567 0 — 3y
0
Hi, thanks for responding again! Unfortunately, this doesn't work either. There's not a leaderstats in the game when I tested it. PadmeOragana 78 — 3y

Answer this question