Confused with UpdateAsync

Hi, Firstly, I am new to scripting.

So I was reading a forum post about update async, the example they provided confused me, like how would I use Update async for a leaderstats value?

local DataStores = game:GetService("DataStoreService")
local CoinDataStore = DataStores:GetDataStore("CoinsDataStore") 

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
 
    --Data loading
    local setSuccessCoins, errorMessage = pcall(function()
		CoinsData = CoinDataStore:GetAsync(player.UserId.."-Coins")
	end)
    if not setSuccessCoins then
		warn(errorMessage)
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
    -- don't know what to put here.
end)

-- I didn't forget bind to close, just didn't put it here.

so how do I use update async to insure that no player’s data is lost?

Another thing to mention I am terrible at datastores.

After looking at the API for 50 hours

UpdateAsync() simply takes the value inside a key (Or Player.UserId) of a DataStore, and “updates” it with the NewValue you want to update it (Player’s current Coins before they leave)

For your PlayerRemoving event, I believe you could do something like this?

game.Players.PlayerRemoving:Connect(function(player)
    local success, whoops = pcall(function()
        CoinDataStore:UpdateAsync(player.UserId.."-Coins", function(OldValue)
            local NewValue = OldValue or 0 --If OldValue is nil, we can set it to 0 to prevent the script from breaking
            return NewValue
        end)
    end)

    if success then
        print("Data updated")
    else
        warn(whoops)
    end
end)
1 Like

So what if the player’s data fails to load can we use the old value instead?

No, the data is the old value. If it fails to load, it’ll return an error.

2 Likes

Oh I see, but then why do people use update async over set async?

what makes update async better then set async?

Typically I don’t use UpdateAsync() as much as I used to because it doesn’t provide many uses over SetAsync() other than reading the previous value. If you don’t need the previous value of the DataStore, there is virtually no reason why you should use it over SetAsync().

The example that @JackscarIitt gave doesn’t do anything more than SetAsync() can do, other than making your code more complicated. For a beginner, I would suggest you stick with SetAsync() and proper error handling.

1 Like

I think I found why

  • UpdateAsync is the canonical way to update data. Get should be used to retrieve data and set should be used when you need to force data. This is also an official recommendation as stated on the Developer Hub.
  • UpdateAsync respects previous data in the DataStore. If you don’t use this, you have to tread fairly carefully when doing a Get-Set method.
  • UpdateAsync respects conflicting calls. Data won’t force itself as the new value or try to overstep other calls that are also attempting to write to the DataStore.

These little “perks” of using UpdateAsync() over SetAsync() are negligible because like I said, unless you need the previous value of a DataStore, there is virtually no reason why you should use it over SetAsync().

Oh, because I was trying to make a secure datastore to insure no data was lost,
I thought update async would help…

I can’t use Datastore 2 because I want leader boards.

Usually, if you have to get the original value and use the new value to change it, you would use UpdateAsync.

This way, you would not need to make two datastore requests.
e.g.

-- instead of this
local coins = store:GetAsync("key")
store:SetAsync("key", coins + 1)

-- you can use this
store:UpdateAsync("key", function(coins)
    return coins + 1
end)
4 Likes

Thanks I’ll be using this in my game :slight_smile:

how would I update it with the leaderstats value?