How do I use UpdateAsync()?

Heres the Devhub code explained.

local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points")
 
game.Players.PlayerAdded:Connect(function(player)
	local playerKey = "Player_" .. player.UserId
	-- Give 50 points to players each time they visit
	local success, err = pcall(function()
		pointsDataStore:UpdateAsync(playerKey, function(oldValue) -- The 'oldValue' is what is currently stored in the datastore.
			local newValue = oldValue or 0 -- If the oldValue is nil it automatically becomes 0
			newValue = newValue + 50 -- Adds 50 to the value
			return newValue -- Returns the updated value to the datastore where the datastore then changes the selected datastore keys value to the new value
		end)
	end)
end)
5 Likes