Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

May someone explain me, what is pcall() and where should I use it?

Asked by 6 years ago

May someone explain me what does pcall() mean and where shall I use it?

2 answers

Log in to vote
5
Answered by 6 years ago

Hi AwesomeDorijan111 you use pcall() when you want to check if a script errors like this:

local function GoodPrint()
    print("Hello")
end

local function BadPrint()
    printtt("Hello") -- throws error
end

local success, message = pcall(GoodPrint)

if success then -- If the good print did not error
    print("GoodPrint successful")
else -- IT ERROR AHH
    print("Error: "..message)
end

local successful, msg = pcall(BadPrint)

if successful then
    print("Bad print success")
else
    print("Error: "..msg) -- attempt to index global 'printtt' (a nil value)
end

Basically your script won't error in the output unless you tell it to. Usually your script would stop wherever it errors. But pcall() lets scripts continue running even if it makes errors.

0
You should also mention that pcall() is short for protected call. saSlol2436 716 — 6y
0
Thanks mate, I learned something new! AswormeDorijan111 531 — 6y
0
Your welcome! This was a good answer. saSlol2436 716 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

In Roblox, there are three different types of error handlers. pcall, xpcall, and ypcall. Each of them run the provided function (for these three it'll always be the first) in a separate thread and suppresses errors. Both pcall and ypcall return the same values. The first value it returns is whether the function succeeded or not. When this value is false, there was an error. When it is true, the function ran successfully. The second value, if the function errored, is the error message. If the function succeeded, the other values will be the values returned by the function call. For a short while in Roblox, you were unable to yield in pcall (aka protected call). ypcall was then added (aka yieldable protected call), allowing you to yield in protected calls. However, to remain consistent with Lua, they added the ability to yield in pcall by (literally) setting ypcall to pcall (for a while when pcall errored it'd tell you the error was coming from ypcall in the stack trace). xpcall, on the contrary, is a bit different. The difference is that the second argument to xpcall is the error handler. The error handler is supplied one parameter, which is the error message. The error handler is only called if the main function errors. xpcall returns almost the same values as pcall. The first is whether the main function ran successfully. If the function ran successfully, then the other values are the values returned by the main function. Otherwise, the other values are the values returned by the error handler.

I hope this helped. You might not be able to completely understand all of this but you can use it as a reference as you start getting more advanced. Also, correct me if you spot an error.

0
This dude made a whole dictionary. Lakodex 711 — 4y

Answer this question