Calling dframe or other vgui element trough two function

I have a little problem for communicate with 2 Dframe on two different function.

for example I have this Derma Function


function UITest()

local DPanelAmmoWindows = vgui.Create( "DFrame" )
DPanelAmmoWindows:SetPos( 100, 100 )
DPanelAmmoWindows:SetSize( 300, 200 )
DPanelAmmoWindows:SetTitle( "" )
DPanelAmmoWindows:SetVisible( true )
DPanelAmmoWindows:SetDraggable( true )
DPanelAmmoWindows:ShowCloseButton( false )
DPanelAmmoWindows:MakePopup()

local DButtonClose = vgui.Create( "DButton", DPanelAmmoWindows )
DButtonClose:SetPos( DPanelAmmoWindows:GetWide() / 2 - 30 , 165)
DButtonClose:SetText( "Close" )
DButtonClose:SetSize( 60, 23 )

----------------------------------------------------
DButtonClose.DoClick = function()
     DermaPanel:Close()
end
----------------------------------------------------

end
-- End Of function UITest() -- 	
concommand.Add("UiTesting", UITest)

and on other one I want to make closing the DermaPanel from UITest2 with the button " DButtonClose "
on UITest


function UITest2()

local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 10, 10)
DermaPanel:SetSize( 300, 500)
DermaPanel:SetTitle( " test" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:SetSizable(true)
 DermaPanel:MakePopup()


end
-- End Of function UITest2() -- 	
concommand.Add("UiTesting2", UITest2)

Thanks in advance.

Does it give any errors?

Try making DermaPanel global(Remove local), and make sure it’s being called before UITest.

It seem to not working.

In overall to be more precise in what I say ,
I have this in one file in example Autorun/TestThisUI.Lua

There is no error with or without global same with local
I think my principal Panel " DPanelAmmoWindows " where is in
UITest() cannot read " DermaPanel " in UITest2()


function UITest2()

local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 10, 10)
DermaPanel:SetSize( 300, 500)
DermaPanel:SetTitle( " test" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:SetSizable(true)
 DermaPanel:MakePopup()


end
-- End Of function UITest2() -- 	
concommand.Add("UiTesting2", UITest2)

function UITest()

local DPanelAmmoWindows = vgui.Create( "DFrame" )
DPanelAmmoWindows:SetPos( 100, 100 )
DPanelAmmoWindows:SetSize( 300, 200 )
DPanelAmmoWindows:SetTitle( "" )
DPanelAmmoWindows:SetVisible( true )
DPanelAmmoWindows:SetDraggable( true )
DPanelAmmoWindows:ShowCloseButton( false )
DPanelAmmoWindows:MakePopup()

local DButtonClose = vgui.Create( "DButton", DPanelAmmoWindows )
DButtonClose:SetPos( DPanelAmmoWindows:GetWide() / 2 - 30 , 165)
DButtonClose:SetText( "Close" )
DButtonClose:SetSize( 60, 23 )

----------------------------------------------------
DButtonClose.DoClick = function()
     DermaPanel:Close()
end
----------------------------------------------------

end
-- End Of function UITest() -- 	
concommand.Add("UiTesting", UITest)

[lua]function UITest2()

DermaPanel = vgui.Create( “DFrame” )
DermaPanel:SetPos( 10, 10)
DermaPanel:SetSize( 300, 500)
DermaPanel:SetTitle( " test" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:SetSizable(true)
DermaPanel:MakePopup()

end
– End Of function UITest2() –
concommand.Add(“UiTesting2”, UITest2)

function UITest()

local DPanelAmmoWindows = vgui.Create( “DFrame” )
DPanelAmmoWindows:SetPos( 100, 100 )
DPanelAmmoWindows:SetSize( 300, 200 )
DPanelAmmoWindows:SetTitle( “” )
DPanelAmmoWindows:SetVisible( true )
DPanelAmmoWindows:SetDraggable( true )
DPanelAmmoWindows:ShowCloseButton( false )
DPanelAmmoWindows:MakePopup()

local DButtonClose = vgui.Create( “DButton”, DPanelAmmoWindows )
DButtonClose:SetPos( DPanelAmmoWindows:GetWide() / 2 - 30 , 165)
DButtonClose:SetText( “Close” )
DButtonClose:SetSize( 60, 23 )


DButtonClose.DoClick = function()
DermaPanel:Close()
end

end
– End Of function UITest() –
concommand.Add(“UiTesting”, UITest)[/lua]

I removed local from DermaPanel and it works fine for me. If you’re calling a variable/element from a separate function you need to make sure it’s global.

Thanks it working now , I made something wrong with a condition earlier.

It’s better to make it local to file if you can.



local DermaPanel
function UITest2()

DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 10, 10)
DermaPanel:SetSize( 300, 500)
DermaPanel:SetTitle( " test" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:SetSizable(true)
 DermaPanel:MakePopup()
end

Also you should add a sanity check to the button click:



DButtonClose.DoClick = function()
     if DemaPanel:IsValid() then DermaPanel:Close() end
end