Clock error

Hello,

I try to integrate a clock into my HUD, which works, but posts his me an error:
Tried to use invalid object (type Panel) (Object was NULL or not of the right type)

I have no idea why the error displays.



local Time = vgui.Create("DLabel", TaskBar) 
	Time:SetPos(1165, 6)
	Time:SetSize(100,35)
local showClock = CreateClientConVar("Clock", "1", false, false)
  local function tictoc()
    if !showClock:GetBool() then return end
    Time:SetText(os.date("%H:%M:%S"))
    Time:SizeToContents()
end 
  hook.Add("HUDPaint", "realtime", tictoc)
	



Thanks you in advance

Strange, works for me without errors. Perhaps you can give more details, such as what line is throwing that error?

[ERROR] addons/littlebob_script/lua/entities/computer/cl_init.lua:185: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)

  1. SetText - [C]:-1
  2. v - addons/littlebob_script/lua/entities/computer/cl_init.lua:185
    3. unknown - lua/includes/modules/hook.lua:84

The line 185 is :



 Time:SetText(os.date("%H:%M:%S"))


you probably removed the Taskbar panel (or whatever it’s parented to) without removing your HUDPaint hook. if you use the DLabel itself as your hook name, then the hook system will automatically remove the hook when the panel becomes invalid. this works with any object with an obj:IsValid() function.

don’t use HUDPaint like that either – all you need is something like
[lua]
function Time:Think()
self:SetText(os.date("%H:%M:%S"))
end
[/lua]
alternatively, you don’t have to create a label at all:
[lua]
function Taskbar:Paint(w, h)

draw.SimpleText(os.date("%H:%M:%S", “Default”, …))

end
[/lua]


 function Taskbar:Paint(w, h)
    ...
    draw.SimpleText(os.date("%H:%M:%S", "Default", ...))
    ...
end


Thank you very much