Are Pcalls needed for UpdateAsync?

Alright so, here is what Roblox says:


“The function will be called as many times as needed until the data is saved.” Alright. So that means no pcall right? But then there is the code sample on the DevHub:

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)
			local newValue = oldValue or 0
			newValue = newValue + 50
			return newValue
		end)
	end)
end)

Looks like they still used a pcall? No error handling though.

So is that code sample wrong because the pcall is not necessary (not even error handling, which makes it even more unnecessary) or do I need a pcall for UpdateAsync?

1 Like

Just because there is no error handling doesn’t make a pcall pointless.

And the general principal is that if you ever are making a request to an outside source you should wrap it in a pcall in case something changes in the future.

I would still wrap it in a pcall and add error handling if you want, the code on the API reference is there so you understand how their built in function works, it doesn’t mean it’s the best way to do it.

3 Likes

Alright, also do you think I should replace SetAsync with just a UpdateAsync that has only a return in it’s function?

I don’t need the oldValue parameter.

ds:UpdateAsync(key, function()
    return value
end)

If the previous value is not crucial for what the new value should be, I wouldn’t bother with it.

I would use UpdateAsync for things like currencies or player experience where you intended to increment the value.

1 Like