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

How do I save player cash with DataStore?

Asked by 3 years ago

What I'm trying to do is save the player's cash I have never used DataStore before so I have no idea what I'm doing.

Here is my attempt:

local DataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
    repeat
    local Cash = DataStoreService:GetDataStore(plr.Name.. "'s cash")

    local success, CashAmount = pcall(function()
        return Cash:GetAsync(plr.Name.. "'s cash")
    end)

    if success then
        print("Current Cash:", CashAmount)
        plr.PlayerScripts.Cash.Value = math.floor(CashAmount / 2)
    else
        repeat
        local success, err = pcall(function()
            Cash:SetAsync(plr.Name.. "'s cash", plr.PlayerScripts.Cash.Value)
        end)

        if success then
            print("Success!")
        else
            print("Failed!")
            end
            until success
        end
    until success
end)

game.Players.PlayerRemoving:Connect(function(plr)
    repeat
    local Cash = DataStoreService:GetDataStore(plr.Name.. "'s cash")

    local success, newName = pcall(function()
        return Cash:UpdateAsync(plr.Name.. "'s cash", plr.PlayerScripts.Cash.Value)
    end)

    if success then
        print("Updated ".. plr.Name .."'s Cash")
    else
        print("Failed!")
        end
    until success
end)

It repeatedly prints out "Failed!" In the output when I playtest it on Roblox Studio I have tried changing the script by adding the repeat loop to make sure it doesn't fail

It is a regular Script and is inside ServerScriptService

0
Are you testing this in Studio? If so, do you have Studio access to API services enabled? appxritixn 2235 — 3y
0
No I did not, Thanks ill try if it works now. CharaAndFriskUTS 0 — 3y
0
This fixed my problem, Thanks. CharaAndFriskUTS 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Nevermind, I have fixed it.

Ad

Answer this question