Derma panel toggle function help?

So I’m doing some Lua coding for Garry’s Mod and I’ve ran in to a small problem. So I have the GM:ShowSpare2() function (F4 button) set to open a derma panel, however I want to set it up so that I call a function once, and it opens the derma panel, then when I call it again, it closes. Can you help me? Thank you very much!


local Menu
function openNewF4()
	Menu = vgui.Create("DFrame")
    Menu:SetPos(ScrW() / 2 - 400, ScrH() / 2 - 400)
    Menu:SetSize(800, 700)
    Menu:SetText("My Menu")
    Menu:SetDraggable(false)
    Menu:ShowCloseButton(true)
    Menu:MakePopup()
 
    local Text = vgui.Create("DLabel",Menu)
    Text:SetText("You opened the menu!")
	Text:SizeToContents()
    Text:Center()
end

function closeNewF4()
	Menu:Remove()
end

function F4Toggle()
	if not Menu then
		openNewF4()
	else
		closeNewF4()
	end
end
hook.Add("ShowSpare2", "F4Hook", F4Toggle)


Why doesn’t this work? Please help me. Thanks

-snip

idk wtf this guy^ is doing but that won’t work

also i think you don’t know what ninja’d means…
[lua]

if ( SERVER ) then
function GM:ShowSpare(player)
player:ConCommand(“openmenu”);
end;
else
concommand.Add( “openmenu”, function( player, command, arguments )
if ( not _MENU ) then
_MENU = vgui.Create(“DermaElement”);

		-- manipulate the panel here.
		
                    _MENU:MakePopup();
		_MENU:SetVisible(true);
		gui.EnableScreenClicker(true);
	else
		if ( _MENU:IsVisible() ) then
			_MENU:SetVisible(false);
			gui.EnableScreenClicker(false);
		else
			_MENU:SetVisible(true);
			gui.EnableScreenClicker(true);
		end;
	end;
end);

end;
[/lua]

I helped someone recently with the exact same problem; check out this thread, it’ll answer your question, and then a few additional points.

http://forum.facepunch.com/showthread.php?t=1357928

Oh my, Acecool! Wow, first (FPtje) Atheos helped me on the DarkRP forums, now you! Thanks, I think I got it figure out now!