HUD Performance Problem?

So, I created a HUD addon. The addon contains only one file and that is a lua file inside the ‘autorun’ folder. Everything works fine. Even the HUD. Except that after a few minutes the game crashes with no crash message. Here is all the code…




if(CLIENT) then

print(HUD_PRINTCONSOLE, "Server is using OMGOMG132's HUD!")

function HideHUD(name)
	for k,v in pairs{"CHudHealth", "CHudBattery"} do
		if(name  == v) then return false end
	end
end
hook.Add("HUDShouldDraw", "HideHUD", HideHUD)

function HUDPaint()
	//self.BaseClass:HUDPaint()
	
	local person = LocalPlayer()
	
	local Health = LocalPlayer():Health()
	
	local Armor = LocalPlayer():Armor()
	
	surface.CreateFont("omg_hud", {
		font = "coolvetica",
		size = 34,
		weight = 400,
		blursize = 0,
		scanlines = 0,
		antialias = false,
		underline = false,
		italic = false,
		strikeout = false,
		symbol = false,
		rotary = false,
		shadow = false,
		additive = false,
		outline = false,
	})
	
	surface.CreateFont("omg_hud_txt", {
		font = "coolvetica",
		size = 34,
		weight = 400,
		blursize = 0,
		scanlines = 0,
		antialias = false,
		underline = false,
		italic = false,
		strikeout = false,
		symbol = false,
		rotary = false,
		shadow = false,
		additive = false,
		outline = false,
	})
	
	//Main drawing code
	
	draw.SimpleText("HUD By OMGOMG132", "omg_hud", ScrW() - 280, 10, Color(255, 0, 0, 180), TEXT_ALIGN_LEFT)
	
	draw.RoundedBox(4, 24, (ScrH()/2)+(ScrW()/4) - 20, 90, 150, Color(20, 20, 20, 200))
	if(Health >= 0) then draw.RoundedBox(2, 30, (ScrH()/2)+(ScrW()/4) - 14, 78, 0 + Health, Color(100, 255, 100, 255)) else end
	draw.SimpleText("Health", "omg_hud", 28, (ScrH()/2)+(ScrW()/4) + 90, Color(100, 255, 100, 255), TEXT_ALIGN_LEFT)
	draw.SimpleText(Health, "omg_hud_txt", 47, (ScrH()/2)+(ScrW()/4) + 20, Color(40, 40, 40, 255), TEXT_ALIGN_LEFT)
	
	local armorAlpha = 255
	if(Armor <= 0) then armorAlpha = 0 else armorAlpha = 255 end
	
	draw.RoundedBox(4, 140, (ScrH()/2)+(ScrW()/4) - 20, 90, 150, Color(20, 20, 20, armorAlpha - 55))
	draw.RoundedBox(2, 146, (ScrH()/2)+(ScrW()/4) - 14, 78, 0 + Armor, Color(100, 100, 255, armorAlpha))
	draw.SimpleText("Armor", "omg_hud", 145, (ScrH()/2)+(ScrW()/4) + 90, Color(100, 100, 255, armorAlpha), TEXT_ALIGN_LEFT)
	draw.SimpleText(Armor, "omg_hud_txt", 164, (ScrH()/2)+(ScrW()/4) + 20, Color(40, 40, 40, armorAlpha), TEXT_ALIGN_LEFT)
end
hook.Add("HUDPaint", "HUDPaint", HUDPaint)

end


as I said… Works fine but crashes after a while. (while being 5-10 minutes.)

Am I doing this wrong? Is there a specific way? I know for a fact my addon is causing the crash since the game doesn’t crash when is not loaded.

You’re creating the fonts every frame.

Take the create fonts out of your drawing function and throw them to the top of the document, then try it.

Yep, everything is working fine now. Thanks guys.