Now the pieces are falling togheter! Tysm all!
No, you cannot use two names for 1 variable. pcall
is a function that returns two values, the first of which, returns a boolean
, while the second returns whatever the pcall throws or returns.
This means that the first value, success
is defined by the boolean
, the first value that is returned by the pcall.
On the contrast, errormessage
, the second value returned from the pcall, is set to the corresponding second value that is returned from the pcall.
local V1,V2 = 34 --Doing this defines only the first value of the tuple, but not the second.
print(V1) --34
print (V2) --nil
local V1,V2 = 34,34 --This will define both variables, since each value has a corresponding definition.
print(V1) --34
print (V2) --34
boy this convoluted i am terrible at explanations
Yes, so actually your doing:
local succesTrueOrFalse, ReturnOfPcall
…and you’ve literally summed up the entire thing. Thank you.
No problem, i really learned something of you guys. Now i am helped enough. Cya all!
Just throwing in my 2 cents here also so others can learn, this is a perfect situation to use xpcall
in.
bool, variant pcall( function f, tuple args )
bool, variant xpcall ( function f, function err, tuple args )
Both functions return two data types: a bool that’s true or false depending on the success of the call, and a potential error message that’s either nil or a string. The difference between pcall
and xpcall
is that xpcall
includes an argument that allows you to pass an error handling function. The value passed to function err
is the error message, which allows you to cut down on the amount of code you write.
Here is an example of how to replicate your example code using it.
function myTest()
...
end
if xpcall(myTest, warn) then
-- protected call ran with success
...
end
xpcall
will return true if our call ran with success, which will allow us to dive into the if
statement. If the call fails, then the error message will be passed to warn
and the code in the if
statement will be ignored because xpcall
will return false
Hopefully this helps
your pcall is very specific. to explain it better, here’s what pcall is:
a way to run code on the condition that if it errors, code doesn’t stop, it just returns the error message. If code is successful, then it return true.