#Help understading UpdateAsync()

1 messages · Page 1 of 1 (latest)

topaz cedar
#

I've been reading about UpdateAsync(), and I am confused by some of its concepts:

For context im trying to use UpdateAsync for a LimitedRedeemCode system, where I will reduce the number of maxredeem when a player redeem the code [100Redeems Left-> Players Redeems a Code -> 99Redeems Left]

I fear that players in other server can also redeem the code at the same time and not properly reduce the RedeemsLeft and cause much more Redeems that it should have and so I explored a way that can make sure that the RedeemLeft data is properly reduced everytime a player uses the code, and so came across UpdateAsync()

I have red that the UpdateAsync makes sure that the data is not overwritten by running the function passed to it again when another server edited the data
(see photo1 from Roblox Documentation)

But ive also come across a dev forum post that we should add an incremental value everytime we edit the data, and in the case that it does not equal to the current increment we will cancel the set, but I don't see the importance of it as UpdateAsync() makes sure that another server cannot overwrite the data, by running again when the data is changed
(see photo 2 from https://devforum.roblox.com/t/stop-using-setasync-to-save-player-data/276457)

PS: I may have commit some mistakes on my current understanding of how UpdateAsync() works and in my question, My current understading about UpdateAsync() is very surfaced level as theres not much documentation around it. Any help is greatly appreciated. Thank you!

old raft
#

This can works ok in some cases but it relies on the fact that we must know what server saves first and we can know this for playerData but we don't always now it in other cases

#

You should only use updateAsync if your using the oldValue

#

this is bad

dataStore:UpdateAsync(key, function(oldValue)
    return newValue
end)

this is good

dataStore:UpdateAsync(key, function(oldValue)
    return oldValue + 1
end)

this is good

dataStore:UpdateAsync(key, function(oldValue)
    if oldValue == 5 then return 3 end
    if oldValue == 25 then return 11 end
    if oldValue == "bob" then return 76 end
end)
somber hawk
#

Why is it good?

#

Also you can add multiple returns?

old raft
old raft
old raft
#

It works just like any function