Well the first value it returns is the success value, it returns if the script successfully runs without errors, the second one catches the errors and then returns the error inside the script.
Alright, I understood the first value but the second one I didn’t. What do you mean with catches the errors and then returns the error inside the script?
And if you want it to do a callback instead of just returning the string of the error you can use xpcall. Which is practically the same thing except that you can add a function to callback if the script errors.
The results of pcall are, if the function errors, false and the error object (could be anything); otherwise, the results are true and all of the return values from the function.
local function f(err,value)
if err then
error(value)
else
return value
end
end
local success,result = pcall(f,true,"abc")
print(success,result) --> false <script>:3: abc
local success,result = pcall(f,false,"def")
print(success,result) --> true def
if the code was not executed without errors, then the first variable will return false
local success = pcall(function()
return nil* "string"
end)
if not success then print("there was an error") end
--> prints, because success was not equal to true
Now to catch the reason an error occurred, use a second variable
local success, err = pcall(function()
print(nil + 5)
end)
if not success then
return warn(err)
end
--// warns this in the output:
--> 18:13:14.252 - :2: attempt to perform arithmetic (add) on nil and number