Is this a good way to use UpdateAsync or is there a better way?

UpdateAsync takes the key that you want to update and requires you to put a callback function.

The description says that the callback function would then set the new value.

That means if I want to update a value, I would have to set something that is like a global variable that the function uses to replace the current value with.

Otherwise the current way I find ideal, just purely as an example, newValue would be something else and not like that how it is in the code.

local DataStoreService = game:GetService("DataStoreService")

local success, updateData, keyInfo = pcall(function()
	local newValue = "test"
	return DataStoreService:GetDataStore("test"):UpdateAsync("testData", function(currentData, keyInfo)
		print(currentData, keyInfo)
		
		local userIDs = keyInfo:GetUserIds()
		local metadata = keyInfo:GetMetadata()
		
		return newValue, userIDs, metadata
	end)
end)

if success then
	print(keyInfo.CreatedTime)
	print(keyInfo:GetMetadata())
	
	print(DataStoreService:GetDataStore("test"):GetAsync("testData"))
end

Because this function let’s me pass the new value within that pcall instead of having to put it somewhere else.

 
Let me know if this way is totally fine. I don’t see an issue with it. Or would SetAsync be fine with it too? I heard that it should be used only if you want to force change something, apparently UpdateAsync seems to update the actual updated value, I think.

yes, I think updateasync is the best way.

setasync would be fine too but there can be any rare problems with it so I think updateasync is the best way. I’m using it too for my leaderstats btw.