“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?
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.