Are nesting connections good practice?

having trouble understanding whether or not nesting connections would be good, for example

local openMyFrameButton = button
local myFrame = Frame
local myFrameCloseButton = myFrame .button

openMyFrameButton.Activated:Connect(function()
    openMyFrameButton.Visible = false --(cant be activated again until after it has been closed)
    myFrame.Visible = true

    local myFrameCloseConnection

    myFrameCloseConnection = myFrameCloseButton.Activated:Connect(function()
        openMyFrameButton.Visible = true
        myFrame.Visible = false
        myFrameCloseConnection:Disconnect()
        
    end)
end)

i know that there is no reason for myFrameCloseButton’s activated connection to exist before openMyFrameButton is called,

but i would need to keep track of all the connections if i do it that way

the more simpler way to do the same thing is this

openMyFrameButton.Activated:Connect(function()
    myFrame.Visible = true
end)

myFrameCloseButton.Activated:Connect(function()
    myFrame.Visible = false
end)

but i know myFrameCloseButton will never be activated before openMyFrameButton as myFrameCloseButton is a child of myFrame.

which is the better way of doing things?

Not sure exactly how you are doing this, but you could make the open button close when pressed

openMyFrameButton.Activated:Connect(function() 
myFrame.Visible = not myFrame.Visible
end)

Other than that, I think nested connections take up more memory (I’m not 100% sure).
Also, in my opinion, nested connections are less readable.

you are unnecessarily adding a connection each time openmyframebutton activates as you already know since you use the disconnect() It would just be simple to not have it nested like that in this case.

I assume it is also impossible to activate openmyframebutton multiple times? not sure how ur gui is set up but if the player can activate it repeatedly, then theres unnecessary connections being added

yes , sorry for not clarifying, you can only activate openMyFrameButton once and only after myFrameCloseButton has been activated, so the connection is only made once

then theres rly no issue, though i think its just easier not to have it nested and declare the connection once outside.

Any type of script will take up memory, fairly contextual I must say.

Nested connections are good practice as they offer you an opportunity to disconnect itself. Roblox states they automatically disconnect connections on destruction though if you have an object that will not destroy ( ScreenGui.ResetOnRespawn = false ), you can still manage when to connect & disconnect events.

Summed up, nested connections are good practice if you have no alternatives and will give you control over your code.

You can do nested connections if you use Once or disconnect them manually

local openMyFrameButton = button
local myFrame = Frame
local myFrameCloseButton = myFrame .button

openMyFrameButton.Activated:Connect(function()
    openMyFrameButton.Visible = false --(cant be activated again until after it has been closed)
    myFrame.Visible = true

    local myFrameCloseConnection

    myFrameCloseButton.Activated:Once(function() -- Automatically disconnects after it fires.
        openMyFrameButton.Visible = true
        myFrame.Visible = false
        
    end)
end)

As for the other question, Events are, well, event based. Your script will not “lag” more just because there is a connection listening to it forever. The simply won’t activate until the event registers. Therefore I’d suggest the second method you gave.

Not only is it simpler but probably takes up less memory, perhaps even faster and performs the same task as the first method.

Connecting/Disconnecting perhaps takes more computing power as well.