Potentially harmful example API usage of GetAsync()

I was reading the example given on GlobalDataStore:GetAsync() and I noticed it has

local myGold
local success, err = pcall(function()
	myGold = goldDataStore:GetAsync(playerKey)
end)
if success then
	gold.Value = myGold
else
	gold.Value = STARTING_GOLD
end

If the datastore was to error when trying to retrieve the data, this code would go on to set the player’s gold as the starting gold amount. This could cause the player’s data to basically be reset.

I believe a safer and more useful way of doing this would be

local myGold
local success, err = pcall(function()
	myGold = goldDataStore:GetAsync(playerKey) or STARTING_GOLD
end)
if success then
	gold.Value = myGold
else
	-- failed to retrieve data
end
12 Likes