Timer only working in console

I’m sorry if this question is idiotic, I am new to coding in Lua and i really cant find a solution to this anywhere.
Basically my issue is that i am creating a timer and it will only count down/up if the console window is open. (Meaning: The bar dose not move and the timer dose not count down or up, but when console is opened the timer counts and the bar moves, however if the console is closed again it freezes.)

Side Note: This is in cl_hud and the console command is called when a weapon hits an entity using primary attack
Code:


GatheringAmmountDone = 0

function GatheringBar()

	hook.Add( "HUDPaint", "GatheringBar", GatheringBar )
	local X = ScrW() / 2
	local Y = ScrH() / 2
	
	timer.Create( "GatheringTimer", 1, 0, function()
		GatheringAmmountDone = GatheringAmmountDone + 1
		Msg(GatheringAmmountDone)
		if GatheringAmmountDone == 10 then
			timer.Destroy("GatheringTimer")
			hook.Remove( "HUDPaint", "GatheringBar", GatheringBar )
		end
	end)
		
	local GathTX = ScrW() / 2
	local GathTY = Y + 7
	local MinWide = 5
	local Width = ScrW() / 4
	local Height = 30
	local PlyGatherMax = math.Clamp(GatheringAmmountDone/10, 0, 1)
	local PlyGatherBarWidth = (Width - MinWide) * PlyGatherMax

	draw.RoundedBox(4, X, Y, Width + 10, Height, Color(0,0,0,200))
	draw.RoundedBox(4, X + 5, Y + 5, MinWide + PlyGatherBarWidth, Height -10, Color(113,0,90,200))
	draw.RoundedBox(4, X + 5, Y + 5, MinWide + PlyGatherBarWidth, 10, Color(200,200,200,3))
	draw.SimpleText(math.Max(GatheringAmmountDone, 0).." / 10","Trebuchet18", GathTX , GathTY , Color(255,255,255,255),1,0)
end
concommand.Add("nrp_gathertime",GatheringBar)

Don’t put your script in lua/vgui.

Ugh, Thanks I will try changing it around and let you know what happens.

**Edit
Ive moved the code into init.lua and it works, thanks a lot :smiley: