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 5 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.

01Players.PlayerRemoving:Connect(function(player)
02    local CoinValue = PlayerCoins:FindFirstChild(tostring(player.UserId))
03    print(CoinValue.Value)
04    local CoinStore = DataStoreService:GetDataStore("CoinStore")
05    local key = tostring(player.UserId)
06 
07    local success, errorMsg = pcall(function()
08        CoinStore:UpdateAsync(key,CoinValue.Value)
09    end)
10 
11    if success then
12        print("yay")
13        print(success)
14    elseif errorMsg then
15        print("nope")
16        print(success)
17    end
18end)

1 answer

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

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

01local Players = game:GetService('Players')
02local DataStoreService = game:GetService('DataStoreService')
03local CoinStore = DataStoreService:GetDataStore("CoinStore")
04 
05 
06Players.PlayerRemoving:Connect(function(player)
07    local key = tostring(player.UserId)
08    local CoinValue = PlayerCoins:FindFirstChild(key).Value
09    print(CoinValue)
10 
11    local success, errorMsg = pcall(function()
12        CoinStore:UpdateAsync(key, function()
13            return CoinValue
14        end)
15    end)
View all 23 lines...
0
Tried that, neither the print under success or else message prints AthenaThorn 4 — 5y
Ad

Answer this question