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?
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?
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
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.
~= 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
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
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
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 0 → false not false → true not nil → true