Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 answers

✓ Accepted answer

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

3 comments

0

Why not just use "if Car ~= fixed then"?

0

~ is not there's no difference between them.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.