✓ Accepted answer
pcall and xpcall catch errors.
They can be usefull for DataStore function as those error alot and with pcall you can catch the error.
Example:
local mon
local success, err = pcall(function()
mon = dataStore:GetAsync(plr.UserId)
end)
if success then
print('datastores succesfully returned data: '..mon)
else
print(err)
end
If the dataStore fails and errors it doesn't stop the rest of the code from executing. Normal function would make the rest of the code stop.
Probably also should note that:
pcall() returns a bool and a string, the bool is true if the function ran without error if not the string will be an error message.
1 comment
pcall and xpcall should only be used to catch exceptions. Exceptions should only happen in exceptional circumstances. In short you use them as little as possible and only when requiored.