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

why won't my leaderstats save?

Asked by 5 years ago
Edited 5 years ago

trying to get the leaderstats to save but its not working, the cash does go up but, the data isn't saving. is there anything im missing?

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("IntValue",player)

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

local cash = Instance.new("IntValue",leaderstats)

cash.Name = "RPCash"

cash.Value = 0

cash.Parent = leaderstats

local reward = 1

while true do

wait(5)

cash.Value = cash.Value + reward

end

DataStore = game:GetService("DataStoreService"):GetDataStore("SaveCash")

local Key = player.userId

local Data = DataStore:GetAsync(Key)


if Data then

cash.Value = Data

end

end)

game.Players.ChildRemoved:connect(function(Player)

local Key = Player.userId

DataStore:SetAsync(Key,Player.leaderstats.Cash.Value)



end)
0
Please indent your code how the hell do expect us to read that mess User#24403 69 — 5y
0
Also why is DataStore a global variable and this code is completely screwed User#24403 69 — 5y
0
we all learn from our mistakes.. i'll keep looking around and see if i find a solution.. thanks :/ A1exTatum 57 — 5y
0
You never got to the saving part because the while true do loop never ends, preventing the code below it from executing. Rheines 661 — 5y
View all comments (3 more)
0
yea u got a point. i'll try something out later. Happy holidays man. A1exTatum 57 — 5y
0
That's completely wrong @Rheines User#24403 69 — 5y
0
Yeah I realized my mistake after Rheines 661 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Wiki's definition on PlayerRemoving:

The PlayerRemoving event fires right before a Player is leaves the game. This event fires before Instance/ChildRemoved|ChildRemoved does on Players, and behaves somewhat similarly to Instance/DescendantRemoving. Since event fires before the actual removal of a Player, this event is useful to store player data using a GlobalDataStore.

Meaning the data wasn't saving because the player had already left. Simply switch the events, and referring back to my comments, please indent your code properly and always use local variables.

local cashSave = game:GetService("DataStoreService"):GetDataStore("SaveCash") -- No need to declare this variable each time a player joins. 
local Players = game:GetService("Players")
local reward = 1 -- Same here.

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local cash = Instance.new("IntValue")
    cash.Name = "RPCash"
    cash.Value = cashSave:GetAsync(player.UserId)
    cash.Parent = leaderstats
end)

Players.PlayerRemoving:Connect(function(player)
    cashSave:UpdateAsync(player.UserId, function(previous)
        -- You're simply updating the data store, use this instead
        -- 'previous' is just the old data

        return player.leaderstats.RPCash.Value
        -- function must return what you want to update with
    end)
end)

while true do
    -- much better than having individual loops 
    wait(5) 
    for _, player in ipairs(Players:GetPlayers()) do
        player.leaderstats.RPCash.Value = player.leaderstats.RPCash.Value + reward
    end
end

Here is some information on :UpdateAsync(), and here for a tutorial written by Link150 on how to properly save your player's data.

Ad

Answer this question