[DERMA] How do I connect one button to two frames?

So, I started to mess with derma yesterday, and it was really interesting. Until I came into to a problem. I made two frames, but I had no clue how I could make them both close at the same time. So I started to ask a few friends and eventually post it in the Garry’s Mod Discussions, but I recieved no answer that actually worked. So now I’m here asking for help.
This is the code I currently have, there are no lua errors or anything.


local Frame = vgui.Create( "DFrame" )
Frame:SetSize(200,1200)
Frame:SetTitle( "Wanker" )
Frame:SetPos(0,0)
Frame:SetVisible( true )
Frame:SetDraggable( false )
Frame:ShowCloseButton( true )
Frame:MakePopup()
Frame.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end

local close = vgui.Create("DButton", Frame)
close:SetSize(100,30)
close:SetPos(50,50)
close:SetText("Close")
close:SetTextColor(Color( 255,255,255))
close.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(41,128,185,250))
	end

close.DoClick = function ()
    close:Remove()
    Frame:Remove()
	gui.EnableScreenClicker(false)
	
	net.Start("CS_MenuClosed")
    net.SendToServer()
end

local Frame = vgui.Create("DFrame")
Frame:SetSize(1137,1200)
Frame:SetPos(783,0)
Frame:SetTitle ("Test")
Frame:SetVisible(true)
Frame:SetDraggable(false)
Frame:ShowCloseButton(true)
Frame:MakePopup()
Frame.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end


And this is what the it looks like:

http://images.akamai.steamusercontent.com/ugc/270599410806487365/C4103A03D8ED647186BEE7EB1F95C4A4F0A0DAE0/

Try moving the click.DoClick section to the bottom and possibly give the second Frame a different variable name

Yeah, the problem is you created two frames with the same variable name, meaning you overwrite it to close the old frame rather than the new one

Give both panels global variables, and do GlobalVar1:Remove() when the button is pressed for both panels.

What do you mean with different variable name? Should I change the DFrame to something else? And if yes, then what?



local var1 = ...
local var2 = ...



local Frame = vgui.Create( "DFrame" )
local var1 = ("Doge")
Frame:SetSize(200,1200)
Frame:SetTitle( "Wanker" )
Frame:SetPos(0,0)
Frame:SetVisible( true )
Frame:SetDraggable( false )
Frame:ShowCloseButton( true )
Frame:MakePopup()
Frame.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end

local Frame = vgui.Create("DFrame")
local var2 = ("Shiba")
Frame:SetSize(1137,1200)
Frame:SetPos(783,0)
Frame:SetTitle ("Test")
Frame:SetVisible(true)
Frame:SetDraggable(false)
Frame:ShowCloseButton(true)
Frame:MakePopup()
Frame.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end

local close = vgui.Create("DButton", Frame)
close:SetSize(100,30)
close:SetPos(50,50)
close:SetText("Close")
close:SetTextColor(Color( 255,255,255))
close.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(41,128,185,250))
	end

close.DoClick = function ()
    close:Remove()
    Frame:Remove()
	gui.EnableScreenClicker(false)
	
	net.Start("CS_MenuClosed")
    net.SendToServer()
end

I did this, but nothing is changed. You have to be a little more specific because I just started :c But thanks anyway

You never changed the variable. Look:



local Frame = vgui.Create( "DFrame" )

-- then you did this later

local Frame = vgui.Create("DFrame")


Just change one of those Frame variables to be a different name

[editline]4th August 2016[/editline]



local Frame = vgui.Create( "DFrame" )
Frame:SetSize(200,1200)
Frame:SetTitle( "Wanker" )
Frame:SetPos(0,0)
Frame:SetVisible( true )
Frame:SetDraggable( false )
Frame:ShowCloseButton( true )
Frame:MakePopup()
Frame.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end

local OTHER_FRAME = vgui.Create("DFrame")
OTHER_FRAME:SetSize(1137,1200)
OTHER_FRAME:SetPos(783,0)
OTHER_FRAME:SetTitle ("Test")
OTHER_FRAME:SetVisible(true)
OTHER_FRAME:SetDraggable(false)
OTHER_FRAME:ShowCloseButton(true)
OTHER_FRAME:MakePopup()
OTHER_FRAME.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(32,36,47))
end

local close = vgui.Create("DButton", Frame)
close:SetSize(100,30)
close:SetPos(50,50)
close:SetText("Close")
close:SetTextColor(Color( 255,255,255))
close.Paint = function(self,w,h)
    draw.RoundedBox(0,0,0,w,h, Color(41,128,185,250))
	end

close.DoClick = function()
    close:Remove()
    OTHER_FRAME:Remove()
	gui.EnableScreenClicker(false)
	
	net.Start("CS_MenuClosed")
    net.SendToServer()
end


[editline]4th August 2016[/editline]

Variables are the simplest thing possible to know in Lua. If you don’t understand them, it’s probably not a good idea to even be touching derma yet.

[editline]4th August 2016[/editline]

What you were doing before was literally equivalent to this:



local var1 = 1

local var1 = 2

print( var1 ) -- This will obviously equal 2 because you literally just set it to 2 mate

-- Why do you expect it to still equal 1 if you set it to be something else?


Thanks man. Though you forgot to add


Frame:Remove()

:c
But thanks so much.