How do I go about using :UpdateAsync() for a donation handler?


I am looking to potentially start using :UpdateAsync(), however I have no idea how it works, and why I would want to use it over :SetAsync(). I want to use :UpdateAsync() as the player won’t ever be removed from the game after the purchase succeeds, which I think could cause some problems. What I don’t understand is that it says it just returns an updated value from, however I don’t understand how it updates the actual datastore? Also, why do I have to use a function instead of just a value as an argument? Also, how would I update a table’s value instead of a single string/integer value?

Also, here’s the site I used:

TIA for the help and answering my questions!

UpdateAsync is intended to mold to current data, not to force data (that’s what SetAsync is used for), so having a function as the second argument gives it more versatility - though I just set the data variables.

Handling UpdateAsync isn’t all too different from handling SetAsync though you’ll have to use your noggin a bit more.

A basic system could look like this:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local PREFIX = "save1_"
local DataStore = DataStoreService:GetDataStore("Data")

	success,errorMessage = pcall(function()
		DATASTORE:UpdateAsync(PREFIX .. tostring(player.UserId),function(savedData) -- savedData = latest value under this data store key
			savedData = player.Data.AmountDonated.Value
			return savedData
	if not success then warn(player.Name,errorMessage) end

If anything, you can use Increment Async since you’ll surely be using a number, and never a table.

1 Like