I've seen previously in the past the uses of the pcall variations; however, some don't appear to work, and sometimes, to me, they don't work (at all).
One usage I've seen is the following:
local player = game.Players.LocalPlayer pcall(function() player.PlayerGui.ScreenGui.TextLabel.Text = 'String' end)
Or, similarly, this:
local Player = script.Parent pcall(function() Humanoid.Health = 0 -- How is that even supposed to work..? end)
But one of the major issues is that it masks errors:
function printWrittenIncorrectly() printttt('Hi! :D') end pcall(printWrittenIncorrectly)
However, you can retrieve the data it masks by doing the following:
function printWrittenIncorrectly() printttt('Hi! :D') end local Fired, Error = pcall(printWrittenIncorrectly) if not Fired then print(Error) else print('Code fired!') end
But this is only one of the uses I'm aware of that'll work properly; but even so, you have to do that to unmask the error it hid.
Back to my question: How does pcall, xpcall, and ypcall actually work?
pcall
takes a function as it's only argument, and runs it in protected mode (hence the name pcall, or protected call). It always returns two values. The first in which is a boolean that determines whether or not the code ran without error (needless to say, true
meaning it ran without error, false
meaning it didn't). The second value depends on the first one. If the code ran without error, the second value is whatever the function you passed to pcall
returns. If it hit an error, the second value is a string
that contains the error message. Example:
local returned, data = pcall(function() print("Hello world") return "Ran without error!" end) print(returned, data)
The example above would print true
and "Ran without error!"
, since there's nothing wrong with our code. However, if I were to input something like this:
local returned, data = pcall(function() x("Hello world") -- Assuming 'x' doesn't exist return "Ran without error!" end) print(returned, data)
It'd print false
and something along the lines of attempt to call x (a nil value)
.
xpcall
works the same way pcall
does, except it allows you to handle the error message it (potentially) returns yourself, by passing a second function argument as a callback. It will return the same error message like pcall
, but as an argument to your new callback. Here's an example:
local returned, data = xpcall(function() x("This will error") end, function(err) -- Second function argument, with parameter that will represent the error message print("Error: ", err) -- Print "Error: " before the message end) print(returned, data)
This would work the same way as pcall
, except it would read "Error: attempt to index x (a nil value)"
As of now, ypcall
is the exact same as pcall
(according to the ROBLOX wiki). It exists, however, because pcall
was never supposed to allow any form of yielding within it's function argument. ypcall
on the other hand, was implemented as an exception to this limitation. Though, the rules have changed since then (see ypcall for more info)
Hope this helped, let me know if you have any questions.
Locked by EmbeddedHorror, JakyeRU, ForeverBrown, WideSteal321, and BlackOrange3343
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?