Welcome curious Developers!
In this tutorial, I’ll be explaining how pcall()
and xpcall()
functions work and what happened to ypcall()
.
Let’s start
pcall()
pcall()
is short for P rotected Call , if the code inside of this function has an error it will yield until the attached function run without any issues and won’t stop the rest of the script. It also returns two values: A bool one and a variant one.
local boolValue, variantValue = pcall(function() -- 1
-- this code is protected
end)
- Naming the bool and the variant value, attaching
pcall()
to anonymous function.
The bool value is true
when the function attached to pcall()
ran without any issues, otherwise, it’s false
; the variant value is a bit more complicated but let me explain as well as I can. When the bool value is false
(error in the code), the variant value will contain the error that can be displayed with orange text in output using warn()
function.
local boolValue, variantValue = pcall(function()
-- this code is protected
return 'hi' -- 1
end)
if boolValue == false then -- 2
warn(variantValue) -- 3
else
print(variantValue) -- 4
end
- Explained below
- Checking if there was an issue in the script.
- This function will be executed when bool is
false
- This function will be executed when bool is
true
warn()
warn()
is like print()
but print()
writes information in the output and warn()
writes warning which have orange text color.
But what will happen when the code inside the function doesn’t have any issues? If you don’t use return
procedure the variant variable will return nil
. You can use the return function if you want to store data from the script into the universal variable.
local successed, errData = pcall(function()
print(1)
print(2)
print(3)
return "string" -- 1
end)
print(errData)
-- Output:
-- 1
-- 2
-- 3
-- 'string'
- You can too return other values.
Use of pcall()
The most popular use of pcall()
is data storing because functions like GetAsync()
or SetAsync()
are network calls and they can generate an error. pcall()
is the best option in this case.
xpcall()
It’s better than pcall() (at least for me) because you can customize the error handler and it’s not yielding , if you know Python you can compare it to try-except block but xpcall is more universal. You have to provide 2 functions and if the first one “fail” the second one will run.
local successed, returnedData = xpcall(function()
-- protected code
end, function() -- 1
print('there is an error')
end)
- This function will run when the previous function got an error, simple right?
ypcall()
Short for “Yielding Protected Call”. Sometime pcall()
wasn’t yielding and then ypcall()
was useful but now pcall()
is yielding too and this is the same thing so ypcall()
is just deprecated and you should not use it anymore.
The End
I recommend reading Lua Globals because there is a short description for some functions and variables (for pcall() and xpcall() too), the rest is in Roblox Globals. That was my first community tutorial in DevForum, hope it’s understandable because English isn’t my strong point. I’d be happy if you give your opinion.
Thanks for reading , have a nice day!