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

How do I make this UpdateAsync for storing a player's coins work?

Asked by 4 years ago

I've already asked the dev of the studio I'm in and we couldn't figure it out. Anyway, Every time, it prints the error (errorMsg from pcall arg). The key is the players ID (not number but string) and CoinValue is a number. I've already SetAsync it so it's a valid datastore entry.

Players.PlayerRemoving:Connect(function(player)
    local CoinValue = PlayerCoins:FindFirstChild(tostring(player.UserId))
    print(CoinValue.Value)
    local CoinStore = DataStoreService:GetDataStore("CoinStore")
    local key = tostring(player.UserId)

    local success, errorMsg = pcall(function()
        CoinStore:UpdateAsync(key,CoinValue.Value)
    end)

    if success then
        print("yay")
        print(success)
    elseif errorMsg then
        print("nope")
        print(success)
    end
end)

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You have to provide a function to UpdateAsync. It will also return the old saved value if you need it.

local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')
local CoinStore = DataStoreService:GetDataStore("CoinStore")


Players.PlayerRemoving:Connect(function(player)
    local key = tostring(player.UserId)
    local CoinValue = PlayerCoins:FindFirstChild(key).Value
    print(CoinValue)

    local success, errorMsg = pcall(function()
        CoinStore:UpdateAsync(key, function()
            return CoinValue
        end)
    end)

    if success then
        print("yay")
    else 
        print("nope")
        print(errorMsg )
    end
end)
0
Tried that, neither the print under success or else message prints AthenaThorn 4 — 4y
Ad

Answer this question