Why is it useful to print out the errormessage?

Hello! I have a question about pcalls in DataStores. So my question is following:
Why would you warn the errormessage after the DataStore has failed to save? Why would you not try it again?

local success, errormessage = pcall(function()
	DataStore:SetAsync()
end)

if success then
	print("Successfully saved!")
	
elseif errormessage then
	warn(errormessage)  --Why? Why not DataStore:SetAsync() again?
end

Why would you not do DataStore:SetAsync() again in hope that it would work this time?
Why should the player care about the errormessage, he just lost his data, lol.

Thank you!

2 Likes

You can retry if you want. And btw errormessage is a string not a boolean.

You print it out so you can actually see what went wrong.

Pcalls are used to catch the errors, if any, without yielding the script. You see, if the Data Store errors and you don’t have a pcall, then the script will just stay there, stopped.

Printing the error message is a way to see what went wrong. Of course, you can always try again. In fact, here is the DevHub article for DataStore saving retries. It’s also good to have an autosave (also in that article).

One thing to add though, use UpdateAsync() instead of SetAsync() since the former considers the previous value and updates it, while the latter only overrides it, which can cause data loss.

2 Likes

The latter is useful if you need to force data. For the former, just mass-replacing all occurrences of SetAsync, ruins the whole point of actually updating it. Idk why SetAsync is so villainized