Need some help with Derma, OnPlayerChat and common sense. (Radio)


local bool = true
local function Radio(ply, msg)
	emsg = string.lower(msg)
	
	if (emsg == "!radio") then
		local Panel = Panel or NULL
			if Panel:IsValid() then
			else
			Panel = vgui.Create("HTML")
			end
		if bool then
			if Panel:IsValid() then
			Panel:OpenURL("http://www.nyan.cat")
			end
			bool = false
			chat.AddText("On")
			
		else
			if Panel:IsValid() then
			Panel:Remove()
			end
			bool = true
			chat.AddText("Off")
			
		end
		return true
	end
end

hook.Add("OnPlayerChat","Radio",Radio)

It manages to create and openurl the panel, but it doesn’t seem to want to remove it when bool is false.



local bool = true
local function Radio(ply, msg)
    emsg = string.lower(msg)

    if (emsg == "!radio") then
        local Panel = Panel or NULL
         if  !bool then
             if Panel:IsValid()  then Panel:Remove() end
            bool = true
            chat.AddText("Off")
        else
            chat.AddText("On")
            Panel = vgui.Create("HTML")
            Panel:OpenURL("http://www.nyan.cat")
            bool = false
        end
        return true
    end
end

hook.Add("OnPlayerChat","Radio",Radio)


Never hits the “Off” option for some reason, is Panel:IsValid() bugging out or…?

Edit: Heh now it hits “Off” option but doesn’t actually remove anything.

[editline]20th February 2013[/editline]

I think i found out the problem. The Panel NEVER turns valid, why? Tried this in a ton of different ways, the panel just never turns valid.

[editline]20th February 2013[/editline]

Sweet got it to work, i forgot gmod doesn’t retrieve old panels automaticall:



local bool = true
local function Radio(ply, msg)
    emsg = string.lower(msg)

    if (emsg == "!radio") then
        LocalPlayer().Panel = LocalPlayer().Panel or NULL
		local Derp = LocalPlayer().Panel
         if  !bool then
             if Derp:IsValid()  then 
			 Derp:Remove()
			 chat.AddText("Never gonna happen")
			 end
            bool = true
            chat.AddText("Off")
        else
            chat.AddText("On")
            new_panel = vgui.Create("HTML")
            new_panel:OpenURL("http://www.nyan.cat")
			LocalPlayer().Panel = new_panel
            bool = false
        end
        return true
    end
end

hook.Add("OnPlayerChat","Radio",Radio)