No grammar found. Anyways, what I was trying to say in the title is how would I make one of the "equal to" statements accept two possible values. Here is an example:
1 | If v = = random 1 or v = = random 2 then |
However, I wanted to know if there was a way I could combine the two. As in have one equal to statement with two possible values (Ie. V == random1 or random2; something like that). How would I do this?
You are completely correct. Use the or
operator.
Like not
or and
, the or operation in if then statements will see if something will equal this, OR if it will equal something else:
1 | local a = 2 |
2 | local b = 1 |
3 |
4 | if a < 1 or b > 0 then |
5 | print ( "1 of these are true" ) --2<1 is not true, but 1>0 is, so the script runs anyway. |
6 | end |
You could also use and
, which would need BOTH of the statements to be true:
1 | local a = 2 |
2 | local b = 1 |
3 |
4 | if a > 0 and b< 9001 then |
5 | print ( "Both are correct!" ) --If one of those 2 statements are false, the script doesn't run. |
6 | end |
Also not
is kinda like ~=. It means not true or does not equal this:
1 | local test = "LordDragonZord" |
2 |
3 | if not test = = "Bob" then |
4 | print ( "LordDragonZord is not Bob." ) |
5 | end |
It runs because it is saying: "If LordDragonZord does NOT equal Bob then..."
Hope this helps!
1 | if v.Name = = 'Hi' or 'Hi' then |
2 | -- the first argument checks for `v`'s Name property, |
3 | --while the second only checks if `string`. |
All conditions are defaulted to true unless explicitly false or nil. In the code you have provided, your second operand
is not a comparison and is also not nil or false, therefore is true.
So, even if the first comparison was false, the following code would run because the logical operator or would check both operands, and as long as one is true, would proceed.
In order to work around that, you would have to precisely state what it's comparing to, which is why you need to do it like so..
You could use tables in the following manner, to check for a match between your condition, and the values in table (table).
01 | table = { 'Condition1' , 'Condition2' } |
02 | if table [ condition ] or table [ another ] then |
03 | --if condition is contained in |
04 | -- table `table` |
05 | -- work |
06 | end |
07 |
08 | -- or |
09 |
10 | if v = random 1 or v = random 2 then |
Yes, u can do it. Here's an example:
1 | local 2 = 0 |
2 | local 6 = 7 |
3 | v = 0 |
4 |
5 | if v = = local 2 or local 6 then |
6 | print ( "Ok" ) |
7 | end |
either you assign v
as 0 or 7, "ok" will be printed