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
9 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:

If 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 — 9y

3 answers

Log in to vote
6
Answered by 9 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:

local a = 2
local b = 1

if a < 1 or b > 0 then
    print("1 of these are true") --2<1 is not true, but 1>0 is, so the script runs anyway.
end

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

local a = 2
local b = 1

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

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

local test = "LordDragonZord"

if not test == "Bob" then
    print("LordDragonZord is not Bob.")
end

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
9 years ago
    if v.Name == 'Hi' or 'Hi' then
    -- the first argument checks for `v`'s Name property,
    --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).

table = {'Condition1', 'Condition2'}
if table[condition] or table[another] then
    --if condition is contained in
    -- table `table`
    -- work 
end 

-- or

if 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 — 9y
Log in to vote
-3
Answered by
davness 376 Moderation Voter
9 years ago

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

local2 = 0
local6 = 7
v = 0

if v == local2 or local6 then
    print ("Ok")
end

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 — 9y

Answer this question