Can you please explain to me what does " not " do in the if statement with an example?
Thanks!
Can you please explain to me what does " not " do in the if statement with an example?
Thanks!
“not” is self-explanatory: it checks if a condition isn’t truthy (anything other than nil
or false
)
local condition = false
if not condition then
-- 'condition' is a falsy value, so this will pass
end
-- ^ this is the same as
if condition == false or condition == nil then
--...
end
Yes
That’s what I thought, but I just wanted to make sure.
Also, take this code as an example
if not game.Workspace:FindFirstChild("Part") then
Does this check if the part doesn’t exist in the workspace?
if it doesnt find Part in workspace then it will count it as true
its the same as this
local part = workspace:FindFirstChild("Part")
if not part then
print("Part is not in workspace")
else
print("Part is in workspace")
end
Also, fun fact, using not
2 times is an easy way to convert anything into a bool value:
local function toBool(x: any): boolean
return not not x
end
print(toBool(5)) --true
print(toBool(nil)) --false
yeah, this is also very helpful for toggling things that have bool values like GUI.Enabled or Frame.Visible (in some cases)
Simple toggle using not
local toggled = false
function toggle()
toggled = not toggled
end
The ‘not’ statement reverts an boolean;
If it’s true, it becomes false; If it’s false, becomes true.
Following this ‘false: true’ logic, when working with it on a if-statement, you’ll basically be checking if that condition is truthy, just like mentioned previously on this thread.
Examples of uses:
local is_true = true
if not is_true then
-- is not true
end
local boolean = false
-- Reverts it
boolean = not boolean
-- boolean is now true
And you can use it on ternary expressions too:
local can_afford = false
print(not can_afford and "You cannot buy this" or "You can buy this")
-- Following the logic of if statements, but on a single line
-- "and" on this expression acts like 'then', while "or" acts as else
-- Additional examples:
local can_afford = true
local is_selling = false
print(is_selling and (can_afford and "You can buy it, its on-sale" or "It's on-sale, but you can't buy it") or (can_afford and "It's not for sale, but you can afford it" or "It's not for sale and you can't afford it"))
----- is_selling: true
-- can_afford: "You can buy it, it's on sale"
-- not can_afford: "It's on sale, but you can't afford it"
----- is_selling: false
-- can_afford: "It's not for sale, but you can afford it"
-- not can_afford: "It's not for sale and you can't afford it"
However, it’s advisable to not be using ternary expressions on values that may be nil because it always ends up on a bug; In this case, you should be using the Luau addition if-then expression.
It works similarly to the if-then-end statement, but doesn’t require the ‘end’ part and is a expression.
Examples with the use of if-then expressions:
1:
local function GetCorrectText(can_afford)
return if can_afford then "You can buy this" else "You can't buy this!"
end
print(GetCorrectText(true)) -- can_afford: true
2:
local can_afford = false
print(if can_afford then "You can buy this" or "You cannot buy this")
Additional informations:
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.