Delete DFrame if key is pressed again

Hello i’m working on an F2 menu but i have a problem.
If the user click again on F2 when Dframe is open,it won’t close DFrame.
So i’m looking for a solution to check if Dframe is open and if user is pressing F2 again to close.

My code to call my DFrame:


util.AddNetworkString( "showadmin" )
function MonMenu( ply ) --Start the function
    net.Start("showadmin")
	net.Send(ply)
end --End the function
hook.Add("ShowTeam", "MonMenu", MonMenu) --Add the hook "ShowHelp" so it opens with F1

Thanks!

Why can’t you just do something like this in your clientside code:



local frame -- make a frame variable to use
-- then later
net.Receive( "showadmin", function()
    if !IsValid( frame ) then -- if the frame variable isn't defined then
       frame = vgui.Create( 'DFrame' ) -- set it as your frame
       -- do all the frame creation stuff
    else -- otherwise
       frame:Close() -- close the existing frame
       frame = nil -- set the variable as nil just to make sure
    end
end )


It may be better to not close/delete it, but rather just hide the menu from view. This way it doesn’t have to constantly recreate itself every single time, only the first time.

Yeah but how i can do that ?

You go to the wiki and search “Visible” or “SetVisible” in the search bar. Tadah

if Menu:IsVisible() == false then
– My derma code here
else
Menu:Hide()
end

That doesn’t work.

Because it will never be visible. You need to define it, then do
Menu:SetVisible( !Menu:IsVisible() )

addons/adminmenu/lua/autorun/client/cl_menu.lua:3: attempt to index global ‘Menu’ (a nil value)

I’ve got this error even if i set it visible.

I wouldn’t recommend setting its visibility to false, but remove, because if someone opens the menu too often, they’ll crash because the menu opens itself multiple times.

only if you dont add a check to see if the menu is already open? you can easily avoid what you are saying.

Yeah but i want a way to check if the menu is open and if close if the player press F2 again
Please guys.

you can remove the menu and then check if valid

or you can remove visibility and check if visible

you define the frame as local before anything else that way it is not nil outside of your panel function

I already done that,but that doesn’t work.

if it is visible then remove it / make invisible.

that is how you do it. if you did that then it would work. obviously you did something wrong

[editline]3rd April 2016[/editline]

this is exactly how you do it

I will try. :wink:

[editline]3rd April 2016[/editline]

Someone can convert my code please ? I can’t get it to work.

[lua]
local frame – make a frame variable to use
– then later
net.Receive( “showadmin”, function()
if !IsValid( frame ) then – if the frame variable isn’t defined then
frame = vgui.Create( ‘DFrame’ ) – set it as your frame
– do all the frame creation stuff
else – otherwise
frame:Close() – close the existing frame
frame = nil – set the variable as nil just to make sure
end
end )
[/lua]

once again, this is exactly how you do it. in fact that code should work

[editline]3rd April 2016[/editline]

your code is fine, read the comments

– do all the frame creation stuff


local frame -- make a frame variable to use
-- then later
net.Receive( "showadmin", function(len, ply)
    if !IsValid( frame ) then -- if the frame variable isn't defined then
    frame = vgui.Create( 'DFrame' ) -- set it as your frame
    frame:SetSize(900, 400)
	local w = ScrW() / 2
	local h = ScrH() / 2
	frame:SetVisible( true )
	frame:SetPos(w-450, h-200)
    frame:SetTitle("La Merguez - Panneau de Modération")
    frame:SetDraggable(false)             
    frame:ShowCloseButton(true)
	frame:SizeToContents()
	frame.Paint = function()
	draw.RoundedBox( 8, 0, 0, frame:GetWide(), frame:GetTall(), Color( 50, 50, 50))
	end
	frame:Center()
    frame:MakePopup()
    else -- otherwise
       frame:Close() -- close the existing frame
       frame = nil -- set the variable as nil just to make sure
    end
end )

For example,this don’t work.

[editline]3rd April 2016[/editline]

Fixed,the problem was the MakePopup.

I have help author.
The problem is :



frame:MakePopup() 


But i would like to have an makepopup do anyone have an solution ?

bump