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

Is there a difference between "if not" and " == to nil" ?

Asked by
Radstar1 270 Moderation Voter
6 years ago

I don't see the difference, and I feel like they can be used interchangeably.

2 answers

Log in to vote
2
Answered by
farrizbb 465 Moderation Voter
6 years ago
Edited 6 years ago

They can be used for the same purpose but not and nil are not the same.The major difference between not and nil is that not is a boolean and nil represents complete nothingness. So you can do

frame.Visible = not frame.visible

but you cant do

frame.visible = nil

Nil can also be used to remove/destroy a variable/script whilst not can't. So if you are using an if statement deciding if something is or not something else use not . If you want to change something to blank/remove it from the game but still want to use it later(nil doesn't completely destroy an object) ,then use nil.Also a variable is nil,before you asign it a value.

Not on the other hand is used to inverse a bool so

Not false = true
Not true = false

basically it's English counterpart.

So the difference between if == not and if == nil is if == not will check if its not whatever follows after so for example

if Car == not fixed then

end

whilst if == nil will check if something is nil or blank.

local Car
if Car == nil then
Car:Destroy()
end

For further knowledge use these links: Nil Not

0
Why not just use "if Car ~= fixed then"? hiimgoodpack 2009 — 6y
0
~ is not there's no difference between them. farrizbb 465 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Truth checks and falsy values
Everything is a lie these days

When you see a bit of code which says

if not value then
  -- Code
end

What it's really saying is "If value is 'falsy', then..."

However, when you see a bit of code which says

if value == nil then
  -- Code
end

What it's really saying is "If value does not exist, then..."


So what is a 'falsy' value?
In Lua, everything is either 'truthy' or 'falsy', which determines how it behaves under truth checks (if, and, or, not, while, etc). The only values which are 'falsy' in Lua are:

  • nil
  • false

And everything else is 'truthy'. This means that when you do not value, it is saying "If value is 'truthy', then false. Else true"

This means that the only difference between the two is that not value will be true for nil and false, whereas == nil will be true only for nil.

Answer this question