PCall Function?

I need someone to clarify if Pcall just catches an error or if it does more like re run the function?

I’m just a bit confused on how it exactly works, does it re run the function for me if it errors or do I have to add extra code to do that

I’m just not sure on what exactly it means if someone could clarify for me that would be amazing :sweat_smile:

12 Likes

pcall does not re-run the function for you, it just doesn’t stop the entire script/function/whatever.

https://www.lua.org/pil/8.4.html

31 Likes

Thanks! :+1:

pcall catches an errors in the function inside it. What that means is that they don’t break the script. Like Kampf said, it doesn’t rerun it ever.

6 Likes

To rerun the function, you just need to get if it is success or not, then use a while loop to rerun it until it is success.

local success, ErrorStatement = pcall( AFunctionToDoSomething )
while not success do
  print("Error: "..ErrorStatement)
  wait() --or a specific time
  success, ErrorStatement = pcall( AFunctionToDoSomething )
end

Note: When the pcall is success, ErrorStatement will become the variable that is returned by the function.

27 Likes

Wow thanks for the help, I can do a lot with this!

1 Like

The correct term for this is a thread.

1 Like

Debatably. It really depends upon if it’s in a coroutine or not.

Just a heads up by the way @Quoteory: Repeatedly trying the same function until it succeeds usually doesn’t work out well and is a bad habit to get into. Try and avoid it if you can.

2 Likes

Doesn’t a coroutine create a new thread?

The thing here is that it depends where exactly you put the pcall, so it’s not necessarily a thread. There really isn’t any reason to nitpick the terminology either. It’s not relevant to the OP and a good number of people aren’t going to understand (or need to understand) that at a basic level.

4 Likes

No, but yes. Without going too far into it, it mimics a thread but isn’t an actual new thread.

Yeah. I thought it was gray area. I think that it’s relevant to this topic though to understand what a thread is.

No it’s not. OP is asking what pcall does. Read my previous response; nitpicking terminology is fairly silly and unnecessary here.

4 Likes

I would like to note that pcall will not stop syntax error from propagating.They are what prevent your code from compiling in the first place

3 Likes

Pcalls don’t rerun the function. Pcalls are genuinely used to prevent an error from breaking the script. But another thing about pcalls is that they have 2 very useful features:

  1. Being able to see if it is a success

  2. Being able to see the error if it doesn’t show one

image

The first variable that you type will return as true or false depending on if it was a success

The second variable will return the error message, but if the function was a success the second variable will always return nil(unless the function returned something)

You were wondering if it reruns the function. It doesn’t automatically do that. But if you put the pcall function inside of a repeat loop, and set it to keep repeating until (the variable you used to determine if it was a success) is true, it will keep doing the loop until success. It will probably look like this:

Hopfully this helped!

4 Likes

Oops, I didn’t realize this post was made 2 years ago.

7 Likes