UpdateAsync() not working? [SOLVED]

(I’m not an expert at DataStores so forgive me for this)

The code I used to reset the data from my DataStores isn’t working. It never seems to work with UpdateAsync and always returns an error message. If I use SetAsync, it works fine, but I’m afraid spamming would cause the requests to clog up.
Script (in ServerScriptService)

game.ReplicatedStorage.resetDataStoreValues.OnServerEvent:Connect(function()
	print("hasran")
	local success, errorMessage = pcall(function()
		cheeseEnding:UpdateAsync("hasCheeseEnding", false)
		timesShoplifted:UpdateAsync("timesShoplifted", true)
	end)
	if success then
		print("Reset DataStores")
	elseif errorMessage then
		warn(errorMessage)
	end
end)

You’re using UpdateAsync incorrectly. UpdateAsync requires a callback to be sent to it as the 2nd argument and it should return the new value:

game.ReplicatedStorage.resetDataStoreValues.OnServerEvent:Connect(function()
	print("hasran")
	local success, errorMessage = pcall(function()
		cheeseEnding:UpdateAsync("hasCheeseEnding", function()
           return false
        end)

		timesShoplifted:UpdateAsync("timesShoplifted", function()
            return true
        end)
	end)
	if success then
		print("Reset DataStores")
	elseif errorMessage then
		warn(errorMessage)
	end
end)

Thanks a lot @HugeCoolboy2007! I’m not a pro at DataStores, so excuse me for that. You’re a lifesaver!

In your case you should be using SetAsync since you’re not transforming the existing data. SetAsync is a write operation whereas UpdateAsync is both a read and a write operation.

Quoted from Roblox Documentation:

GlobalDataStore:SetAsync() is best for a quick update of a specific key, and it only counts against the write limit. However, it may cause data inconsistency if two servers attempt to set the same key at the same time.

GlobalDataStore:UpdateAsync() is safer for handling multi-server attempts because it reads the current key value from the server that last updated it before making any changes. However, it’s somewhat slower because it reads before it writes, and it also counts against both the read and write limit.

My game is going to be a single-player game with multiple servers. So I don’t want any data inconsistency on the player’s part as I would hate to ruin the experience.

In what instance would you have multiple servers querying the same datastore key simultaneously?

1 Like

My game has endings, which have to be saved on true and false values. Many people (probably not) will most probably be triggering the DataStore saves at once. This script is for resetting the values of the DataStores.

So I don’t want any data inconsistency on the player’s part as I would hate to ruin the experience.

I think you’ve misunderstood the documentation you’ve quoted.

is safer for handling multi-server attempts because it reads the current key value from the server that last updated it before making any changes.

UpdateAsync is only going to be more favorable than GetAsync in cases where either multiple servers are attempting to write to the same datastore key or a datastore key’s existing value needs to be transformed.

For simple Boolean value assignments SetAsync will suffice.

1 Like

Oh. Thanks, @Forummer.
So, just to clarify, if a person has a GlobalDataStore, named “hasStolen”, should I use SetAsync or UpdateAsync?