I Have seen people use it in many scripts, I just dont know what that would be used for, any Help?
How would you use the "or" Variable in a script?
3 answers
To add on to 25564's answer, you can also use and and or to approximate the C ternary comparison operator, allowing you to reduce lines of code that would be used in if-elseif chains.
For example:
local someCondition = true
local a = someCondition and 5 or 6
print(a) --> 5
This works because of how Lua deals with and and or.
For clarification, someCondition can be any conditional statement (what you put between the if and then of an if statement.)
and will return the first parameter if it is false or not truthy (aka nil), otherwise it will return the second parameter. or will return the second parameter if the first is false or not truthy, otherwise it will return the first parameter.
To spell this out for you, you first need to understand the Lua order of operations, or operator precedence.
Using this, and and or are given the least precedence, and in that order, so line 3 of the above has its parentheses re-added to make this:
local a = (someCondition and 5) or 6
Which is more clear. Since someCondition is true, the second parameter of the and is returned, in this case 5, which makes the code look like this:
local a = 5 or 6
Which is even more clear. Since 5 is truthy, the or will return it and ignore the 6.
Now let's flip the case and make someCondition false.
local a = someCondition or 6 --remember, someCondition is now false, so it gets returned from the and instead of 5
In this case, since someCondition is false, the second parameter of the or gets returned, in this case 6.
This use of and and or can also be nested or 'chained'.
local cond1 = true
local cond2 = false
print(cond2 and 1 or cond1 and 2 or 3)
--which reduces to
print(cond2 or 2 or 3)
--which reduces to
print(2 or 3)
--which reduces to
print(2)
To begin with "or" is not a variable it is a logical operator as is "and". These operators can be used to create rather advanced conditional statements an example of which is:
a=7
if a == 10 or a > 0 then
print("Statement is true") -- True because while a is not 10 it is greater than 0
else
print("Statement is false")
end
As you can see above "or" requires one of the conditions to be true while "and"
a=7
if a == 10 and a > 0 then --This time with and instead of or
print("Statement is true")
else
print("Statement is false") -- This statement would be false because a is not 10
end
Even though in the above code a is equal to 10 the condition is not met because "and" requires both conditions to be met
A simple example of when logic like this may be required could be in a door that requires you to be lv 30 to pass or you need to have purchased VIP, however this is only a single example to the millions of possible uses these operators have.
2 comments
== is the comparison. Your snippets have typos
My apologies. Fixed them
Just saying,or is not a variable, it's a keyword
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
"To add on to 25564's answer.." [1.337k characters later]