UpdateAsync or SetAsync

I’m cache-ingthe player’s data everytime they join the game and saving the cache everytime I leave.

Should I use updateasync or setasync?

1 Like

I think their data stores are broke

SetAsync is what you want to use to keep players data for next time, UpdateAsync is for when a key (or someones data updates, it’ll call the callback)

So in my case, im setting the whole data, I assume use Set Async?

Yes, correct.

make sure you use a key that matches with the player, the user id is the best way to go about this. E.g SetAsync(player.UserId, TheirData)

1 Like

That’s OnUpdate. UpdateAsync runs a function that cannot yield, passes in the old value as the only argument and expects to be returned with a new value created by the function.

SetAsync or UpdateAsync can be used to save players data for another session. It’s more preferable that UpdateAsync is used in the case that multiple instances are referencing the player’s data, in which their stuff can be overwritten.

I personally only use SetAsync to force data to stick with players, otherwise I use UpdateAsync so things are relatively streamlined across instances.

3 Likes

UpdateAsync is used so that keys can be modified relatively in arbitrary order instead of being set based on what the server stored. Since servers might try to assign data to the same key at the same time (for whatever youre trying to do) it will apply the updates based on which one gets there first. This way instead of permanently setting the value to oldsave+5, it would be set to oldsave+5+5+5… because of the function

function(oldsave) return oldsave+5 end

I typically use SetAsync because i have not found a reason to use UpdateAsync yet. I think an example might be people can plant crops that stay until the server dies (bad example but). They would earn cash from the crops in different servers. This would not work very well if the cash they had was stored on the server and SetAsync was used because servers may have older data values and create conflicting saves.

5 Likes