Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

Tried that, neither the print under success or else message prints

This discussion is preserved as it was. New answers, comments, and votes are closed.