Hud makes me crash.

I recent made a HUD and everytime I run it clientside on my server it makes me crash and it makes my friend crash, anyone have this happen to them before or know a fix? Thanks.

Post your code if you want a fix.

local tohide = {
[“CHudHealth”] = true,
[“CHudBattery”] = true,
[“CHudAmmo”] = true,
[“CHudSecondaryAmmo”] = true
}
local function HUDShouldDraw(SGDHud)
if (tohide[SGDHud]) then
return false;
end
end
hook.Add(“HUDShouldDraw”, “SGDHud”, HUDShouldDraw)

local Panel = vgui.Create( “DPanel” )
Panel:SetPos( 10, 10 )
Panel:SetSize( 200, 200 )

local icon = vgui.Create( “DModelPanel”, Panel )
icon:SetSize( 200, 200 )
icon:SetModel( LocalPlayer():GetModel() )

function SGDHud()
local ply = LocalPlayer()
local HP = ply:Health()
local Armor = ply:Armor()

draw.RoundedBox( 4, 30, ScrH() - 180, 370, 155, Color(255, 255, 255, 255) )

draw.RoundedBox( 4, 165, ScrH() - 150, 200, 40, Color(40, 40, 40, 150) )
draw.RoundedBox( 4, 162.5, ScrH() - 152.5, 204, 44, Color(0, 0, 0, 255) )
draw.RoundedBox( 4, 165, ScrH() - 150, math.Clamp( HP, 0, 200 )*2, 40, Color( 220, 108, 108, 255) )
draw.RoundedBox( 4, 165, ScrH() - 150, math.Clamp( HP, 0, 200 )*2, 15, Color( 255, 255, 255, 30) )

draw.RoundedBox( 4, 165, ScrH() - 100, 200, 40, Color(40, 40, 40, 150) )
draw.RoundedBox( 4, 162.5, ScrH() - 102.5, 204, 44, Color(0, 0, 0, 255) )
draw.RoundedBox( 4, 165, ScrH() - 100, math.Clamp( Armor, 0, 200 )*2, 40, Color( 0, 63, 255, 255) )
draw.RoundedBox( 4, 165, ScrH() - 100, math.Clamp( Armor, 0, 200 )*2, 15, Color( 0, 161, 255, 30) )

surface.CreateFont( "MyFont", 
                {
                font    = "coolvetica",
                size    = 25,
                weight  = 400,
                antialias = false,
                shadow = false
        })

surface.SetTextColor( 0, 0, 0, 255)
surface.SetTextPos( 210, 940 )
surface.SetFont( "MyFont" )
surface.DrawText( "Health: "..HP )

surface.SetTextColor( 0, 0, 0, 255)
surface.SetTextPos( 210, 990 )
surface.SetFont( "MyFont" )
surface.DrawText( "Armor: "..Armor )

end

hook.Add( “HUDPaint”, “SGDHud”, SGDHud )

Don’t define fonts inside the HUDPaint hook.

So how should I change it new to this?

Define the font once outside of the function.

Will this stop crashes?

Maybe, if you dont have anything else dumb going on in your hook

Read this, common mistakes: https://dl.dropboxusercontent.com/u/26074909/tutoring/optimization/_generalized_optimization_guide.lua.html

Also, please use


 tags or [lua] tags so we can see the code with proper highlighting and tabs...