Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

If or statement not even working?

Asked by 2 years ago

Ok so I was working was wokring on this one part for a Chess Game I'm making. When I ran into a small little error which I couldn't really place. So I created a separate script to work it out and now I'm even more confused.

--Conceptual Problem
local NumVar = 2
local NumVar2 = 8
local Answer = 1
if Answer == NumVar then
    print("Horay!1")
end

if Answer == NumVar2 then
    print("Horay!2")
end

if Answer == NumVar or NumVar2 then
    print("Should Not Ever print")
end

This is what I tested in my separate script. Now I was under the impression that because the variable Answer is not equal to either NumVar or NumVar2, the print("Should Not Ever print") would never print. Yet for some reason it does, and I can't quite figure out why. Anyone have any ideas?

1 answer

Log in to vote
2
Answered by
Antelear 185
2 years ago

Here is a bit confusing and I completely understand the confusion. But here's the thing...This is not the ideal way to use if/or statements.

Explanation

Using a logic gate you would find that or is a way of checking if either thing is true. The right way to have done it would have been:

if Ans == NVar or Ans == NVar2 then bla bla bla

The reason being is that OR looks for if either one is true, because you only did ...or NumVar2 then... it will just print because you didnt give any extra information, just that other variable. By default it's true since well that's hard to explain for me at my current knowledge but it's not false so it will print.

Just be careful when doing something...Here's some advice:

What TO do with OR:

if variable/boolean/number == condition or variable/boolean/number == condition then

what NOT to do:

if variable/boolean/number or variable/boolean/number == condition if variable/boolean/number == condition or variable/boolean/number

Look up on conditional statements and experiment with how they work, you'll eventually get it! Mark this answer if it helped :3

0
Thanks man, that does make alot of sense. And frankly I'm surprised I never figured this out having been learning with Lua for well over a year and a half now generalYURASKO 144 — 2y
0
Hey we all make mistakes lol Antelear 185 — 2y
Ad

Answer this question