Unable to remove Derma Frame

Hi, I’m getting a error when I try to make a Derma Fram, and then try and remove the frame.

Here is the error I’m getting



[ERROR] addons/nodecore/lua/autorun/dankmemes.lua:127: attempt to index global 'panel2' (a nil value)
  1. DoClick - addons/nodecore/lua/autorun/dankmemes.lua:127
   2. unknown - lua/vgui/dlabel.lua:232


Here is the code that is giving the error:



fbtn.DoClick = function()
local panel2 = vgui.Create("DFrame")
panel2:SetTitle("")
panel2:ShowCloseButton( false )
panel2:SetSize( 10, 10 )
panel2:Center()
panel2:SetSizable( false )

function panel2:Paint(w, h)
draw.RoundedBox(3, 0, 0, w, h, Color(0, 0, 255, 255))
end
end

ffbtn.DoClick = function()
panel2:Remove()
end


You’re declaring panel2 locally inside a function and trying to remove it outside of that function, when the value panel2 no longer exists.

Why are you creating panel2 in fbtn’s DoClick function?



fbtn.DoClick = function()
local panel2 = vgui.Create("DFrame") -- What


This might work:



local panel2 = vgui.Create("DFrame")
panel2:SetTitle("")
panel2:ShowCloseButton( false )
panel2:SetSize( 10, 10 )
panel2:Center()
panel2:SetSizable( false )

function panel2:Paint(w, h)
    draw.RoundedBox(3, 0, 0, w, h, Color(0, 0, 255, 255))
end

ffbtn.DoClick = function()
    panel2:Remove()
end


[editline]29th October 2017[/editline]

ninja’d

Well I need it to be removed and then created again because I’m making a small dot in the center of the screen as a cross hair.

define panel2 outside of the fbtn DoClick function