I understand that UpdateAsync() is safer than SetAsync(), but what’s the difference?
SetAsync() sets the value of a key, and overwrites any data existing in that key.
UpdateAsync() retrieves the value of a key from a data store and updates it with a new value.
SetAsync
does just what it says – it sets the value of the datastore regardless of the original value. UpdateAsync
takes its second parameter as a function, which allows for the old value to be taken into account (the function is called with the old value as input and is expected to return the new value).
If you’re not using the second parameter of UpdateAsync
as a function, there’s no difference. If you don’t care about the old value, there’s no difference. This is only useful if you want to increment the old value, or save the old value in a separate datastore, or really do anything else with the original value.
One good part about UpdateAsync
is that it doesn’t overwrite the old value if it returns nil
, so if you function somehow messes up then it won’t wipe someone’s data.
The wiki has much more in depth information about both UpdateAsync
and SetAsync
.