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

If this equals to that or another?

Asked by
IXLKIDDO 110
10 years ago

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:

1If v == random1 or v == random2 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?

1
Look at all 3 answers! We all have something different to say! EzraNehemiah_TF2 3552 — 10y

3 answers

Log in to vote
6
Answered by 10 years ago

You are completely correct. Use the or operator.


or Operation

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:

1local a = 2
2local b = 1
3 
4if 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.
6end

You could also use and, which would need BOTH of the statements to be true:

1local a = 2
2local b = 1
3 
4if a > 0 and b< 9001 then
5    print("Both are correct!") --If one of those 2 statements are false, the script doesn't run.
6end

Also not is kinda like ~=. It means not true or does not equal this:

1local test = "LordDragonZord"
2 
3if not test == "Bob" then
4    print("LordDragonZord is not Bob.")
5end

It runs because it is saying: "If LordDragonZord does NOT equal Bob then..."



Hope this helps!

0
`not test == "Bob"` evaluates to false, because it first evaluates `not test` which is false, and then checks if this this new boolean is equal to a string. theCJarmy7 1293 — 6y
Ad
Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
10 years ago
1if 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).

01table = {'Condition1', 'Condition2'}
02if table[condition] or table[another] then
03    --if condition is contained in
04    -- table `table`
05    -- work
06end
07 
08-- or
09 
10if v = random1 or v = random2 then
0
Lol you edited your script thanks to my comment :3, I don't know why someone thumbs this down, the information is correct. I thumbs up your answer, I got your back :3 EzraNehemiah_TF2 3552 — 10y
Log in to vote
-3
Answered by
davness 376 Moderation Voter
10 years ago

Yes, u can do it. Here's an example:

1local2 = 0
2local6 = 7
3v = 0
4 
5if v == local2 or local6 then
6    print ("Ok")
7end

either you assign v as 0 or 7, "ok" will be printed

1
No, you don't even need v to be 0 or 7. Because "or local6" means if local6 exists then..., which it does, you should edit it to say, "if v == local2 or v == local6 then". You had a slight error EzraNehemiah_TF2 3552 — 10y

Answer this question