I’m a little confused in logical operators but not too much like for example I kinda get And, or but I don’t really get not statements. Hope this made sense.
Um… They are pretty much, kind of the Operators that control your scripts.
Here is a Good Documentation/Tutorial that may help you figure this out in a better way:
x and y
will evaluate as y
if x
is truthy, otherwise it will evaluate as x
and not evaluate y
(short circuiting)
x or y
will evaluate as y
if x
is falsy, otherwise it will evaluate as x
and not evaluate y
(short circuiting)
not x
will evaluate as true
if x is falsy, otherwise it will evaluates as false
false
and nil
are falsy, everything else is truthy.
Some good examples can be found here: lua-users wiki: Operators Tutorial
Truth tables help visualise it:
Input | Output | ||||
---|---|---|---|---|---|
A |
B |
and |
or |
and not |
or not |
false |
false |
false |
false |
true |
true |
true |
false |
false |
true |
true |
false |
false |
true |
false |
true |
true |
false |
true |
true |
true |
true |
false |
false |
The not operator basically is Used to reverse the logical state of its operand.
A basic example of this in use.
local someVariable -- Variable is declared but has no value, so it's nil
if not someVariable then
print(someVariable ) -- Outputs "nil" since nil is not true
end
Control Structures | Documentation - Roblox Creator Hub – You can use that as a source maybe.
Logical Operators (Symbols)
if A == B then
will check if variable A has the same value as B
if A ~= B then
will check if variable A does not have the same value as B
if A > B then
will check if variable A is greater than B
if A < B then
will check if variable A is less then B
if A >= B then
will check if variable A is greater than or equal to B
if A <= B then
will check if variable A is lesser than or equal to B
Relational Operators (Keywords)
if A then
will check if variable A has a value or if A is true
if not A then
will check if variable A is false
if A or B then
will first check if variable A is true or has a value, if A is false, it will check if B is true or has a value
if A and B
will check both variable A and B if true or has a value
That’s a lot of true
If statements always seek for a condition that is true. Like…
if not A
if A = true, the if statement will not work, becasue it is looking for a variable that has a value of false or will result to false.
Examples
local A = true
local B = true
if A == B then
A = not B --A will become false because of the `not`
end
local A = true
local B = false
if A or B then
print("One of them was true")
else
print("None of them was true")
end
local A = true
local B = true
if A and B then
print("Both are true")
else
print("At least one or all of them is false")
end
local A = true
local B = false
if A == not B then -- this could also be `if A ~= B then`
print("A is not equal to B")
else
print("A is equal to B")
end
A == not B
is different from A ~= B
local A = 1
local B = 2
print("A ~= B is "..tostring(A ~= B))
print("A == not B is "..tostring(A == not B))
A ~= B
is equivalent to not (A == B)
local A = 1
local B = 2
print("A ~= B is "..tostring(A ~= B))
print("not (A == B) is "..tostring(not (A == B)))
Also, operators aren’t globals?
print(getfenv()["and"]) --> nil
This was specific to my code example
I’m not really sure what to call it if it’s in word form. I’m gonna search that up
Keywords.