How can you properly and efficiently use UpdateASync to update everything in the DataStore? I checked on the Wiki and I just cannot wrap my brain around it. Any help is valued greatly, thanks!
How to use UpdateASync properly?
1 answer
Sometimes reading the Wiki can be painful... I understand!
Okay, for your question...
UpdateAsync is a method for storing a Player's data in DataStoreService.
There are 2 parameters you must mention when using UpdateAsync. The key for the player's entry (Key) and a function that returns the updated value for the key.
Let me show you an example:
local ds = game:GetService("DataStoreService"):GetDataStore("PlayeData") -- Name the DataStore whenever you like!
game.Players.PlayerRemoving:Connect(function(plr)
local key = "plr-"..plr.UserId -- Use UserId instead of the player's name because the player's name may change overtime!
ds:UpdateAsync(key, function(oldvalue) -- Imagine the value is a number...
local newvalue = oldvalue + 100 or 10 -- just to make sure that oldvalue isn't new, or else just treat it as 10.
return newvalue -- Returns and updates the value!
end)
end
To be honest, using UpdateAsync is useful if the old values may be important! If you don't want the old values, use SetAsync which directly uses the value as the second parameter instead of a function!
For example (in one of the many lines in the script), ds:SetAsync(key, 500). This directly sets the value to 500. Of course, that would be kind of ridiculous!
That's all I can tell you. If you have anymore questions, please ask me in the comments below. Thank you and I hope this will help you!
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
So will that method run through the whole DataStore or will I have to call it more than once?
it runs through the whole thing, just make sure u mention the KEY and the script will find the DATA you need!
So it will update everything in the DataStore?
only the value of the player's KEY, not all of the players' data.
Thats what I meant. Like, under the players key, it will update all the of the stuff thats under their key?
ya