Does short-circuiting an IF statement exist in lua?

  • What are you attempting to achieve? (Keep it simple and clear)
    So, in C++ there exists a feature where the checks in an if statement will determine if the next check will be executed.
    Simply put it check if the check is true then it proceeds to the next check, so on and so forth.
    If the check is false, it does not proceed to the next check and potentially volatile code can be made less dangerous.

  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)
    Not really an issue, but an example:

if (1 ==1 && 1/0 = 1){
std::cout << "This will not error";
}else if(1/0==1 && 1==1){
std::cout << "This will error";
};
  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)
    I wanted to ask here to potentially fork this into a feature request if it doesn’t exist, so that code can be made slightly more concise…
1 Like

I would suggest you try these things before you ask.
Short circuiting works fine in Lua. You can test this yourself very easily.

image

2 Likes
if true then

elseif (function() print("Test") end)() then

end

(nothing printed)

if false then

elseif (function() print("Test") end)() then

end

prints Test

So, yeah, there’s short-circuit evaluation for if-statements in Lua.

3 Likes

Why is there no error here:
image
put then why is there an error here:

That is not valid Lua syntax. You have to store the result of that operation in a variable, or put it in an if condition.

5 Likes

So dividing by zero is an operation that is supported by lua?

Yeah, it’s just infinitely high.
image
Your problem had nothing to do with the fact you were doing division, it was just the fact you weren’t doing anything with the result.

1 Like

Oh thank you, i guess im just not used to lua maths.

1 Like