:SetAsync() v.s. :UpdateAsync() which one should I use?!

When making datastores, should I use :SetAsync() for saving player data? Or :UpdateAsync() for saving player data?

7 Likes

Similar threads have already been made on this subject, I suggest you use the search bar next time.

A good answer to the question nonetheless ^.

4 Likes

Your question is answered in this thread, which you could’ve found by searching the keyword “SetAsync” in the search bar.

3 Likes

I would use UpdateAsync() to save players data because from my knowledge using this function correctly helps avoid data loss. Using SetAsync() for saving data can be hazardous because it doesn’t take into account previously saved data so it just overrides the data already saved. On the developers hub it states you should be using UpdateAsync() when updating data because it takes into account any previous saved data:

SetAsync() can be hazardous since it overwrites any value currently in the entry. If you’re updating an existing entry, UpdateAsync() is recommended because it considers the old value before making changes.

I would check out the tutorial that @TheFurryFish suggested because it answers your question really nicely.

11 Likes

I would like to note that there are use cases for both.

It does what it says on the tin. If you’re trying to update data (where your new value depends on the old value), then use UpdateAsync. If you’re trying to set data (overwriting the old value), then SetAsync is more performant.

If you don’t want to think too much then using UpdateAsync for everything is a safe default, because SetAsync is just a special case of UpdateAsync where you don’t care about the old value.

6 Likes

This isn’t a great description of what it does. Often people just say it “prevents data loss,” but I think it’s important to be specific on how it does so. It can be misleading to say it simply prevents data loss.

Depending on the game and situation, this might not be very useful, and might not prevent data loss at all. Generally, a good way to take advantage of UpdateAsync is keeping track of the changes that occurred during a session and applying that to the old data is “safer” than simply overwriting. For example, something like a wins/kills counter will only go up, so you can simply add the number from that session to the old data. This will help if what you’re saving is something constantly increasing, but for something like money that fluctuates UpdateAsync might be harder to use properly.

As another example where you might not want to use UpdateAsync('s old data), let’s say you have some money. You have $40 saved, but let’s say the default amount for players without data is $100. When your data doesn’t load, that means you might spend more in that session than you have saved. Obviously, putting the player in debt isn’t what they would want, since then they’d have to work themself out of it next time, but you can’t just throw out the whole session or people would be upset too. Whichever way it goes there, some data is sure to be lost, whether it’s just the single session or all of their data.

Of course, SetAsync isn’t much better off in this situation, other than removing any ambiguity on what it’ll do. UpdateAsync is often what you should be using, but you should be careful making sweeping generalizations like “it prevents data loss.”

13 Likes

I think the most succinct way you could put it is that UpdateAsync gives you the opportunity to prevent data loss. You can still lose data if you don’t correctly take advantage of that opportunity.

5 Likes