Who can help me? I have this datastore script, but it dorsn't save the Cash

local ds = game:GetService(“DataStoreService”):GetDataStore(“SaveData”)
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = “id_”…plr.userId
local save1 = plr.leaderstats.Cash
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
ds:UpdateAsync(“id_”…plr.userId,{plr.leaderstats.Cash.Value})
end)

That’s the script, it doesn’t save the money and I don’t know what’s the problem.

1 Like

Firstly, always use a pcall() when accessing the data store. It is a network request, so it can throw errors. Secondly, try not using UpdateAsync(). Thirdly, I can’t seem to find where you have created that Money value in the player’s leaderstats.

--my script
local function load(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"

    local playerData
    local success, err = pcall(function()
        playerData = dataStore:GetAsync("id_"..player.UserId)
    end)
    if success and playerData then
        money.Value = playerData["Money"]
    elseif not success and err then
        load(player)
    end
end

local function save(player)
    local saveData = {["Money"] = player.leaderstats.Money.Value}
    
    local success, err = pcall(function()
        dataStore:SetAsync("id_"..player.UserId, saveData)
    end)
    if success then
        print("Saved!")
    elseif not success and err then
        save(player)
    end
end

game.Players.PlayerAdded:Connect(load)
game.Players.PlayerRemoving:Connect(save)

Please note I have not included default data for new players here.

1 Like

No, it won’t wait forever, it will just wait no time.

1 Like

Wait with no arguments will wait the shortest time it can wait.

1 Like