Simple Question
I need help on understanding what the UpdateAsync does in a datastore.
Simple Question
I need help on understanding what the UpdateAsync does in a datastore.
:UpdateAsync
has a callback that passes the value of the data you are updating. You are expected to return the new value in the callback. Example:
dataStore:UpdateAsync(key, function(value)
print(value) -- Prints the current value of the data
return value + 1 -- Will add one to the value of the data
end)
UpdateAsync gives the data from a key in a function so you can act accordingly with the previous data by comparing it with the data that is going to be saved.
What the people said above ^ Update async allows developers to compare values In a data store. It’s like :GetAsync and :SetAsync combined together. Then you can use a return to change the value in the data store.
Say you have a IntValue in a data store and you want to add one to it each time it is called. If you didn’t use Update async, you would have to Get the value with :GetAsync, then add one to it, then use :SetAsync to update it. However by using UpdateAsync, it gives you the value in the datastore, and when you return, it updates the value in the data store so all you would need to do is OldDataStoreValue + 1.
You can learn more on the Roblox docs page
so, how would i do that with a table? it’s quite hard to understand. just do a quest table for a example please.
upon receiving the value(table), find what you want to update and return the updated table
Well like a common thing people do is inside their table that will have a data Version and do what I mentioned above but with a table. Here is an example
local ExampleTableOfWhatAlreadyIsInDataStore = {
[“DataVersion”] = 1,
[“AnotherValueIdk”] = true,
}
DataStore:UpdateAsync(pls.UserId, function(pastData)
pastData[“DataVersion”] += 1-- I get What is inside the datastore already, and add One to it
return pastData -- this returns the old data, And saving it in the datastore, although with the added value in the line above where I add one. If you have new data, you could put it here and add some lines if you wanted to compare data beforehand, and change values based on the old data
end)
So is this what a UpdateAsync function is supposed to look like?
datastore:UpdateAsync(key, function(pastData)
pastData = {
["Purchases"] = {
[ItemType] = {
[itemName] = "Equipable"
}
}
}
return pastData
end)
are you sure you are updating it correctly? the backpacks/reflect backpack module couldve been edited wrong
Thank you a lot. And everyone here I had gotten it!
What’s was the issue and How did you fix it. Just curious, but I’m glad you understand UpdateAsync more!
So, I had put this as my code
datastore:UpdateAsync(key, function(past)
past = {
["Money"] += 12
}
return past
end)
What I did to fix it.
datastore:UpdateAsync(key, function(past)
past["Money"] += 1
return past
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.