What is an if not
statement? I see it in scripts. I think it might mean not equal to or not nil.
You have the right idea, but, let me explain.
The not
keyword is opposite to what it means, if not condition == argument then execute end code
, it checks in the if then end
statement if the condition
is opposite to the argument, here, let me show you an example;
local Identifier = true --Here is our identifier, 'Identifier', it is saving a 'boolean' value [true] if Identifier then --This will check to see if 'Identifier' is set to true, it if it is, it will return print("Identifier is",Identifier) --This will print in the Output if 'Identifier' is true elseif not Identifier then --Now, here comes in the 'not' keywork, it is checking if 'not Identifier', or, if Identifier is false, this could also be used for 'BasePart', and other type instances; This is basically saying 'if not Identifier == true then' print("Identifier") end --This ends the block of code for the 'if' statement
The not keyword is as the code above says, it checks the opposite way from the if then end
statement, you could also use the keyword not
in Identifiers
, let me show you another example;
local Identifier = not true --Identifier 'Identifier' is now saving the boolean 'not true' print(Identifier) --Output: false --Surprized? The 'not' keyword does the opposite for the code, and can be used to different kinds of conditions/arguments, here's another example local Identifier2 = Instance.new("Part",game.Workspace) --Surprized yet? Yep, you can do this will 'BasePart' type instances aswell; 'Identifier2' is now specifying, or indentifying, the new 'BasePart' type instance if not Identifier2 then --The 'if' statement is now checking if 'Identifier2' is not existant print("Where did 'Part' go?!") --This will print into the Output if 'Identifier2' is not existant elseif Identifier2 then --Elseif 'Identifier2' is existant then print("Yay! :D") --This will print into Output if 'Identifier2' is existant end --This ends the block of code for the 'if' statement
Or, to try and dumb the not
keyword down a bit, it checks if condition is not equal to argument
. There are so many possibility's that you can use the not
keyword for, .lua always has a trick up it's sleeve. Haha!
Hope this helped!
The Not Operator is a way to check if something is not the post-not argument. Not
will return true if the post-not argument is nil, or differentiated from how it's defined, it will return false otherwise. if not part then
is the same as if part == nil then
. Some examples of what the not operator will return are;
not true
-false
not false
-true
--say part.Transparency is 1
not part.Transparency == 0
-true
-EDIT-
So basically the not operator is a way to check if something isn't a certain way.. And it returns true or false, true if it ISNT the certain way, false if it IS. You can read more about it here.
I would like to add something that I feel was left out of previous answers.
You should know that the condition of if statements must be true in order to continue the code. It will never pass the statement if the condition is false.
if true then --Will run
-
if false then --Will not run
The not
keyword reverses a boolean value, making all true
values equal false
, and all false
values equal true
.
print(not true) --Output: false
-
print(not false) --Output: true
Therefore, the following is correct;
if not true then --Will not run
-
if not false then --Will run
The reason not true
will not run and not false
will run is because not true
is the same as simply putting false
, and not false
is the same as simply putting true
. You already know that if statements only run if their condition is true.
not false = true
not true = false
The not
keyword is useful in the following way;
local part = workspace.Part if part.CanCollide then--CanCollide must be true for condition to be true if not part.CanCollide then--CanCollide must be false for condition to be true
This is all because not
reverses a boolean value.
Pretend you are trying to check something, so when you say if not, you're basically saying the opposite of if.
Basically not
checks if something is not true
, in other words, false
.
Say you have a variable part
set to nil or the variable part
just doesn't exist. You do
if not part then -- code end
and it would execute the [code]. It's exactly the same as saying
if part == nil then -- code end
Now, say you have part
set to some part in Workspace. Then if you did
if not part then -- code end
it would NOT execute because part is not nil.
Locked by theCJarmy7, MessorAdmin, brokenVectors, and Amiaa16
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?