When and how often to use :UpdateAsync()?

Hello, I have recently started trying to learn how to work with UpdateAsync rather than SetAsync, and I am a bit confused as to how you use the function to optimally save data as it is being updated. From my understanding on UpdateAsync you are supposed to use it whenever updating a value such as “money” in a datastore. Let’s imagine we have a brick with a clickdetector and every time you click on it you get +50 money and that gets updated with UpdateAsync every click. But here is where I get confused, how are you supposed to handle updates when it happens very quickly, if the player were to continually press the brick in quick succession, then the UpdateAsync requests that occur every click would just get throttled. So my question is what is the optimal way and time to use the function in this scenario?

1 Like

Use caching.

1 Like

Welcome to the club! UpdateAsync is a wonderful function.

Ideally you will still be using UpdateAsync as much as you did SetAsync, so that’s scarcely. You can use it at any point that you need to update data but do be wary of any values that you want to have increment instead of just be hard-set. You can make use of the transform function to properly increment or tack on some values as you like.

In terms of updating game data throughout the session, you should employ caching strategies. That meaning, you store a player’s save data on the current server in a dictionary and every time that player clicks to get money you update their money value in the cache. Only after they leave or the server shuts down should you call UpdateAsync to push their data.

2 Likes

Alright, thank you! I figured it’d be something like that, thanks for the quick response!

Which would I want to use for different situations, updateAsync or setAsync?

If you only ever need to set a value once or if the previous value doesn’t matter, SetAsync. Do be careful that if multiple servers are modifying a key it may cause race conditions. If this is the case, then use UpdateAsync like SetAsync instead.

If the previous value is important or if multiple servers might be trying to make changes to one key, UpdateAsync. In all cases of player data you should be using UpdateAsync, never SetAsync.

1 Like

Really? I was using set vs update every time a player left the game, because previous data did not matter…