My friend just achieved something I can’t comprehend. He did something like this:
ImageButton.MouseButton1Click:Connect(function()
InventoryFrame.Visible = not InventoryFrame.Visible -- this simple line works like a toggle
end)
I’m just can’t understand how this works fine at all, i came up with the theory that it works like the negative * negative = positive and positive * negative = negative.
I didn’t find a forum about this and it keeps getting weird, cuz that code just works with BUTTONS, even if you pass the player to a remote event it wont work the same way…
Just to clarify: It’s the InventoryFrame.Visible = not InventoryFrame.Visible bit that you’re confused on?
If that is the case:
It might help if the line is expanded a bit:
if InventoryFrame.Visible == true then
InventoryFrame.Visible = false
else -- InventoryFrame.Visible == false
InventoryFrame.Visible = true
end
Alternatively, you can try thinking of it as if there is a separate variable somewhere in it:
local CurrentlyVisible = InventoryFrame.Visible
InventoryFrame.Visible = not CurrentlyVisible
-- OR
local CurrentlyVisible = InventoryFrame.Visible
if CurrentlyVisible then
InventoryFrame.Visible = false
else
InventoryFrame.Visible = true
end
(not just changes true to false, and false to true.)
pretty much if InventoryFrame.Visible is true, not InventoryFrame.Visible is false and vice versa
so it’s setting InventoryFrame.Visible to the opposite of itself
its just a simple boolean toggle
heres two different approaches to visualize it
in luau
InventoryFrame.Visible = not InventoryFrame.Visible
-- basically says: if visible is true, then it will be the opposite value of when it gets called.
-- lets say you call it when visible is false. you will set it to the opposite of false (which is true)
in boolean algebra
not X - flips the value of X not true - false not false - true
internally, it works as (-1) * X
so when you do this on the .Visible Property, you’re basically doing: (-1) * -1 = 1 and (-1) * 1 = -1
also this doesnt just work with buttons, it works with literally any boolean toggle
thx for the explanations, I’m just confused that i tried to replicate this using remote event passing the player value but it still didn’t work… i tried even AI to explain me cuz didnt found any posts talking about. I know it’s actually obvious what it does and explaining it could resume to just saying that it is how a switch/ toggle works, but can’t understand why it just worked with buttons for me…
anyone can show me how i actually put it to work with remote event? I basically tried firing it by one client passing the player and the server passes the same player to a FireClient after (input → morph → Show UI)