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

How Do I Save/Load Cash Within A GUI?

Asked by 7 years ago

Been trying at this for hours but I can't figure out what the issue(s) could be with the scripts. Note:I realize I didn't include making an leaderboard for it because the intValue is under a TextButton

Saving Script

local DataStore = game:GetService("DataStoreService"):GetDataStore("MoneyData")
local MoneyHolder = game.StarterGui.SaveDataTest.MoneyHolder
game.Players.PlayerAdded:connect(function(player)

    local MoneyData = Instance.new("IntValue",MoneyHolder)
    MoneyData.Name = "Money"

    local key = "player-"..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        MoneyData.Value = savedValues[1]
    else
        local valuesToSave = (MoneyData.Value)
        DataStore:SetAsync(valuesToSave, key)
    end
end)

Loading Script

local DataStore = game:GetService("DataStoreService"):GetDataStore("MoneyData")

game:BindToClose(function()
game.Players.PlayerRemoved:connect(function(player)

    local key = "player-"..player.userId

    local valuesToSave = player.PlayerGui.SaveDataTest.MoneyHolder.Money.Value
    DataStore:SetAsync(valuesToSave, key)
end)
game:BindToClose(function()
wait(3)
end)

Thank you for your help!

0
The `PlayerRemoved` listener (which should be `PlayerRemoving`) won't be connected until the game closes. So, the data doesn't save (your "save" and "load" labels are swapped in this question). Pyrondon 2089 — 7y

Answer this question