I only know pcall, but when I wrote it in the studio, I sometimes got the name of xcall or ycall, are they better than pcall and should I use it?
1 Like
pcall and ypcall are the same. ypcall was added long ago to allow yielding inside of a pcall, but pcall was then patched to yield as well.
xpcall allows you to set a custom error handler for when the function errors. useful for custom error logic.
example:
xpcall(function(...)
print("[xpcall] got", ...)
error("this should error")
end, function(e)
print("an error occured", e)
warn('[traceback]', debug.traceback())
-- // more logic here
end, "a", 2, true, nil, {}, Vector3.new(0, 5, 0))
output:
4 Likes
Can you make it so that if there is an error that it automatically improves or that if there is an error that it then plays a function?
You can do whatever you want in the error handler, so sure. A note for xpcall, it cannot yield iirc
2 Likes
I don’t get it, could you please write a more normal function?
Exemple:
xcall(function(--???)
--Don't understand.
end
Edit: I find the Lua Globals Page. It means that (I’m just trying):
local Bool = true
local function Errorfunction()
print("error😢")
end
local function Sucessfunction()
print("sucess🙂")
end
local sucess, err = xpcall(function(), Errorfunction)
--something
if sucess then
Sucessfunction() --if it is true (sucess), then play the Sucessfunction
elseif err then
err --it run the Errorfunction function if it is error
end
end)
2 Likes
xpcall takes 3 arguments
- the function you want to run in protected mode
- the error handler (will only run if the function errors!)
- the arguments to pass to the function (only in roblox iirc)
local errored = false;
xpcall(function(...) -- ... is used when you want to accept any number of arguments.
print(...)
error("asd")
end, function(error)
errored = true
print("the function errored!", e)
end, 1,2,3,"asd")
-- the first function is the function we wamt to catch errors from
--the second function is our error handler, which allows us to handle the error in amy way we want (logging, retrying, etc)
-- the 1,2,3,"asd" are the arguments we pass to function one
if errored then
print("errored :(")
else
print("not errored")
end
Functions | Documentation - Roblox Creator Hub (should help you understand how to use …)
7 Likes