Quick Advice Needed

Simple question

Would I get the same results if I structure my code as

if party1 and party2 == true then
print("Both parties are true")
end

Compared to

if party1 == true and party2 == true then
print("Both parties are true")
end

Would I get the same results?
Which way is the correct way and why is the other way incorrect?

I’m teaching myself how to code, Ive been doing it for a while. But I haven’t really understood if this
really matters or not.

Both ways work. If you test the game, you should see the message appear 2 times. That means both works. So in theory, yes, the order doesn’t matter. Not in all cases, though.

1 Like

Here’s a little fun fact, if statements by default check if the value isn’t nil or false, if it is anything else it will go through, so you can just do this:

local var = true

if var then
   print("true!")
end

same as doing:

local var = true

if var ~= nil and var ~= false then
    print("true!")
end

What i’m saying is that you don’t need to check on neither, you can just do:

if party1 and party2 then
print("Both parties are true")
end
1 Like

Thank you for your replay and info!

Thank you for your replay and info!

Wrt “Not all cases”: This way works as long as reading the value of party2 has no side effects. If party1 is false, then party2 will not be read as its result doesn’t matter, so any side effects of reading party2 will not be triggered. This will almost never apply, though, so don’t worry about it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.