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

What is an "if not" statement? [closed]

Asked by 10 years ago

What is an if not statement? I see it in scripts. I think it might mean not equal to or not nil.

1
yeah it justs mean not equal, you could do if 1 + 2 ~= 4 then or if not 1 + 2 == 4 then NinjoOnline 1146 — 10y
0
Why do you have answers from 'LordDragonZordTest'? Were you attempting to boost your reputation? DigitalVeer 1473 — 9y
0
For the record, `not 1 + 2 == 4` would error, as it applies `not` to 1, turning it into false, and then you try to add 2 to it, which errors, `not (1 + 2 == 4)` would evaluate to true.  theCJarmy7 1293 — 6y

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?

6 answers

Log in to vote
-4
Answered by 9 years ago
Ad
Log in to vote
7
Answered by 10 years ago

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!

0
Very well explained(: +1 Goulstem 8144 — 10y
0
Lol, thanks. :) When I answer a Question, I try to explain as best I can. :) Your explanation wasn't bad either man, keep up the good work. :) TheeDeathCaster 2368 — 10y
0
This helped me a lot thanks! EzraNehemiah_TF2 3552 — 10y
0
I know this's old, but think of the `not` operator as `~=`. (if not argument == blah then, if argument ~= blah then) TheeDeathCaster 2368 — 7y
Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

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.

0
still a little confusing. EzraNehemiah_TF2 3552 — 10y
0
I'll edit and try to be more clear.. Goulstem 8144 — 10y
0
`not part.Transparency == 0` always evaluates to false, `not (part.Transparency == 0)` is what you're looking for theCJarmy7 1293 — 6y
Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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.

Log in to vote
2
Answered by 10 years ago

Pretend you are trying to check something, so when you say if not, you're basically saying the opposite of if.

0
It doesn't exactly work that way. If you do "if not part then" It doesn't work. EzraNehemiah_TF2 3552 — 10y
Log in to vote
2
Answered by
Tkdriverx 514 Moderation Voter
10 years ago

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.