Questions about the "not" statement

if a bool variable is using a “not” statement for example

local Opened = false

if Opened == false then
 
end
-- Instead of that ^ i want to use the "not" statement because it looks cleaner but i'm still confused.

if not opened then  -- I want this to be "If opened == false" but Would it be "If opened == true" because it's a not statement?
2 Likes

I’m very lost can you explain what you are trying to use the code for?

1 Like

i think not means if its not there or is nil but thats me

1 Like
if not opened then

…will check if ‘opened’ is false, regardless of the current value of ‘opened’.

1 Like

i just want my scripts to look cleaner lol

2 Likes

You can flip whatever the current boolean is:

local opened = false

opened = not opened

And then do something based on the current state:

if opened then

else

end
1 Like

does it work with all bools? or only variables

2 Likes

Do note that booleans have different behaviors compared to other variables.


local str: string = "what"

if str then -- prints because string exists (acts like if str ~= nil then)
	warn("what")
else
	warn("what2")
end

vs

local bool: boolean = false

if bool then 
	warn("what")
else
	warn("what2") -- prints because bool == false
end

if bool ~= nil then -- for bools if you want to check if it's nil or not, you'd have to do this
	warn("what3")
end
1 Like

Quick question why do you do local variablename: instance instead of local name = instance

1 Like

It’s basically typechecking, it’s completely optional and won’t affect anything on your script except tell what ‘type’ a variable is (which allows the autocomplete to display functions and properties specific to that type).

You can read more about it here.

1 Like

You’re complicating this more than necessary, not just flips a Boolean value.

not false --true
not true --false
local exp = 1
not exp --false (1 is truthy).
exp --true (1 is truthy).
1 Like