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

Can someone help me with this simple DataStore script?

Asked by 9 years ago

It's a simple script. but I'm new to DataStore so I'm not sure if I'm doing it right. When I check to see if it saved the cash, it loads a lower number and not the number of money I had before. So say I had 1400, but when I rejoined I only had 900. How would I make this better?

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

function PlayerEnter(player)    
    local board = Instance.new("IntValue",player)
    board.Name = "leaderstats"

    local Cash = Instance.new("IntValue",board)
    Cash.Name = "Money"

    local key = "user_" .. player.userId
    if StoreCash:GetAsync (key) ~= nil then
        Cash.Value = StoreCash:GetAsync (key)
    else 
        Cash.Value = 500
    end
    Cash.Changed:connect(function(val)
        StoreCash:SetAsync (key, Cash.Value)
    end)
end

game.Players.PlayerAdded:connect(PlayerEnter)

1 answer

Log in to vote
0
Answered by 9 years ago

I think you should move

    Cash.Changed:connect(function(val)
        StoreCash:SetAsync (key, Cash.Value)
    end)

outside PlayerEnter function.

Ad

Answer this question