Derma opening 10 times instead of just once

I’m using this code to open a derma panel on the player’s screen when they press E on an npc:



hook.Add( "PlayerUse", "dermat", function( ply, ent )
    if ( ent:GetClass() == "prof" ) then
        RunConsoleCommand("dermapop")
    end
end )

concommand.Add("dermapop", function()
local tFrame = vgui.Create( "DFrame" )
tFrame:SetPos( ScrW()/2 + 50,ScrH()/2 + 50  )
tFrame:SetSize( 300, 150 )
tFrame:SetTitle( "Name window" )
tFrame:SetVisible( true )
tFrame:SetDraggable( false )
tFrame:ShowCloseButton( true )
tFrame:MakePopup()
end)


I intended for it to open only 1 window, but it opens multiple in the same spot making the player have to spam the close button to get rid of all of them. How do I make it only create 1 window?

GM:PlayerUse, as stated on the wiki, continuously runs until USE is released. So, that command is ran until the button is released, hence why it runs multiple times, multiple derma windows.

Check if the window is open before you run the command, like this:
[lua]
local tFrame

hook.Add(“PlayerUse”, “dermat”, function(ply, ent)
if (ent:GetClass() == “prof” and not IsValid(tFrame)) then
RunConsoleCommand(“dermapop”)
end
end)

concommand.Add(“dermapop”, function()
tFrame = vgui.Create(“DFrame”)
tFrame:SetPos(ScrW() / 2 + 50, ScrH() / 2 + 50)
tFrame:SetSize(300, 150)
tFrame:SetTitle(“Name window”)
tFrame:SetVisible(true)
tFrame:SetDraggable(false)
tFrame:ShowCloseButton(true)
tFrame:MakePopup()
end)
[/lua]

You can also make a check inside the command, before you create the frame.

Thanks! I put this into the command:


 

concommand.Add("dermapop", function()
if IsValid(tFrame) then print("the window exists") end

if not IsValid(tFrame) then 
tFrame = vgui.Create( "DFrame" )
tFrame:SetPos( ScrW()/2 + 50,ScrH()/2 + 50  )
tFrame:SetSize( 700, 350 )
tFrame:SetTitle( "Professor" )
tFrame:SetVisible( true )
tFrame:SetDraggable( false )
tFrame:ShowCloseButton( true )
tFrame:MakePopup()

end
end)


You can also add self:SetUseType(SIMPLE_USE) in the ENT:Initialize function of your entity.



function ENT:Initialize()
    self:SetUseType(SIMPLE_USE)
end


Thats for SENTS, what the user is doing is on a hook, but it would be simpler if only that prop needs it.