Hello.
I’m looking for a way to try for a code.
If code runs successfully, it returns true, else, it returns false.
Anyways to do this?
Thanks in advance,
Mehdi
Hello.
I’m looking for a way to try for a code.
If code runs successfully, it returns true, else, it returns false.
Anyways to do this?
Thanks in advance,
Mehdi
pcall
it. Pcall returns whether it ran without errors or not.
Anyways you could send a link to Developer Hub or explain how pcall()
works?
pcall
as first return always returns a boolean. This determines whether the function threw an exception. true
if the function ran successfully, false
otherwise. The second return value, if pcall
returned false
, is the exception message. If the function did NOT fail, the results after the first are the returns of the function.
Here are some examples:
local successful, exception_data = pcall(function()
return { } + 5
end)
print(successful, exception_data)
--> false attempt to perform arithmetic on a table value
If the function did NOT fail…
local successful, data, other_data = pcall(function()
return 0, 1
end)
print(successful, data, other_data)
--> true 0 1
Yeah, here’s the wiki article. Basically it calls the function it’s provided in a safe environment. If it errors, it’ll return false. Otherwise, it’ll return true as well as whatever the function returns. Here’s some examples:
local success1 = pcall(error)
local success2 = pcall(print)
local success3, value = pcall(function(x) return x+3 end, 4)
print(success1, success2) --> false, true
print(success3, value) --> true, 7
edit: it’s function not funcyion