Why ~= nil is better than not?

Here’s a quick example of what’s meant.

I want you to imagine the following lines of code is in a while loop.

local FoundParts = workspace:FindPartsInRegionWithIgnoreList(region, ignoreTable, math.huge)

if FoundParts then
    print(FoundParts.Name)
end

Now, for me, if I try to run this, the region will detect nothing, even though when there’s a part inside it. It would just say nothing was found in the region. However, if I replace the keyword not to ~= nil

local FoundParts = workspace:FindPartsInRegionWithIgnoreList(region, ignoreTable, math.huge)

if FoundParts ~= nil then
    print(FoundParts.Name)
end

It does print out whatever that’s found inside the table. Why?

1 Like

Unless you are going for micro optimizations it’s irrelevant and is most likely personal preference since FoundParts isn’t a boolean.

Despite it being better or not, ~= nil and not is technically different.
The not includes false and nil while ~= nil only checks if nil or not.

So you’re saying, not checks if the value is FALSE and NIL at the same time?

Yes.

local a = nil -- this line is technically useless
local b = false

if not a then
	print'a is nil'
end

if not b then
	print'b is false'
end
> a is nil
> b is false

TL;DR

if checks goes through if the condition isn’t nil and isn’t false

I don’t understand, you literally said it checks FALSE and NIL before passing the conditional statement. But instead, you just give one value. For example, the variable a you’ve made, only has a false value, and it doesn’t have the nil value together. How does this still get past the conditional statement if you said not only pass a conditional statement when the value has both false and nil?

Yes it is,
As an example:

if not (1 + 1 == 1) then
    print("Not")
end
-- Prints "Not"

if (1 + 1 == 1) == nil then
    print("Not")
end
-- Prints nothing because '1 + 1 == 1' is false, not nil

It checks if it’s either nil or false

not Checks if the value is EITHER false or nil, which is what the example @heII_ish has given. There can’t be anything that is both false and nil, they can only be one or the other from my knowledge.

2 Likes

And ~= nil only checks if the value is NIL, REGARDLESS of any other bool values?

~= nil only checks if the value is NOT nil, meaning there is something in that variable/operation. If the variable/operation gives a true or a false, it will proceed, but if it’s nil, it wont proceed in the if statement

local a = nil
local b = false

if a ~= nil then
	print("I wont be printed because a is nil")
end

if b ~= nil then
	print("I will be printed because b is not nil")
end

@hell_ish @EmbatTheHybrid thank you for your answers. Now I’ve understand well.

To fully summarise this post,

if variable/operation then 
	-- Checks if variable/operation is true or not nil
end

if not variable/operation then 
	-- Checks if variable/operation is false or nil
end

if variable/operation ~= false then
	 -- Checks if variable/operation is not false, meaning if it's true or nil it'll proceed. Works for other types, including true and nil
end

if variable/operation == true then 
	-- Checks if variable/operation is true, meaning if it's false or nil, it won't proceed. Works for other types, including false and nil
end

More information is found In the Developer Wiki’s Operators page

you can also use not to optimize your code.

example:

local v = false
if v == false then
    v = true
else
    v = false
end

to

local v
v = not v

That is correct but I’m unsure if that example you gave for not will work as v doesn’t have a value initialized with it, as you kept it as local v instead of something like local v = false or local v = true

for not nil is like a false, undefined local v is always nil, changing v to false or nil in this case is pointless.

2 Likes

So in this case, if you do not nil, the resulting value is false? Sorry if I’m asking what seems to be stupid questions, I never really saw not nil before

If the operand of not is nil (treated like false) or false, it will evaluate to true. Any other type of operand value like strings, numbers, userdata will evaluate to false because they are all treated like true.

For example:
not "foo"false
not 0false
not falsetrue
not niltrue

I see, thanks for clearing that up for me!