Crash 30 seconds after running script

While making a HUD it started crashing shortly after loading the HUD and I have no clue on what in the code is make it crash


function myhud()
	local client = LocalPlayer()
		if not client:Alive() then return end
			if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	local HP = client:Health() --Hp Client has
	local AR = client:Armor() --Armor Client has
	-- HUD Base
	surface.SetDrawColor( 0, 0, 0, 120)
	surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
	-- HUD Font
	surface.CreateFont( "FontULine", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = true,
} )
	surface.CreateFont( "Font1", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = false,
} )
	--Red HP- HUD Precents
	surface.SetFont( "FontULine" )
	surface.SetTextColor( 255, 0, 0, 255) 
	surface.SetTextPos( 100, 1116 )
	surface.DrawText( HP )
	--Blue Armor-
	surface.SetTextColor( 0, 0, 255, 255) 
	surface.SetTextPos( 100, 1146 )
	surface.DrawText( AR )
	-- HUD HP Slider
	surface.SetDrawColor( 128, 128, 128, 200)
	surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
	surface.SetDrawColor( 77, 77, 77, 200)
	surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround Shade
	surface.SetDrawColor( 255, 0, 0, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide  
	surface.SetDrawColor( 255, 117, 117, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade 
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
	-- HUD AR Slider
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
	surface.SetDrawColor( 0, 0, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
	surface.SetDrawColor( 117, 117, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
	-- HUD Alerts!
	surface.SetTextColor( 255, 0, 0, 255)														
	surface.SetTextPos( 200, 1116 )
	surface.SetFont( "Font1" )
	if HP <= 20 then surface.DrawText( "Heath Low!" ) end
	surface.SetTextColor( 0, 0, 255, 255)
	surface.SetTextPos( 200, 1146 )
	if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end										
	if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end 	
end
hook.Add("HUDPaint", "myhud", myhud)


You are creating a font each frame, move these fonts outside of the function

How would I do that

Instead of



function myhud()
// ...
 surface.CreateFont( "FontULine", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = true,
// ...
end


Do this



 surface.CreateFont( "FontULine", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = true,

function myhud()
// ...
end


How would I do that and make the function be able to use in the original function

[editline]29th August 2014[/editline]

That does not work the function does not get the font for surface.DrawText

Just try it. The problem is that each frame, you are creating a new font. Just create it the once, and use it after that as many times as you want.

got a error

[ERROR] lua/hud.lua:19: ‘(’ expected near ‘myhud’

  1. unknown - lua/hud.lua:0

We don’t know your code, what you did, so i’ll give you a vague response

You commited a syntax error…


 surface.CreateFont( "FontULine", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = true,

function myhud()
	local client = LocalPlayer()
		if not client:Alive() then return end
			if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	local HP = client:Health() --Hp Client has
	local AR = client:Armor() --Armor Client has
	-- HUD Base
	surface.SetDrawColor( 0, 0, 0, 120)
	surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
	--Red HP- HUD Precents
	surface.SetFont( "FontULine" )
	surface.SetTextColor( 255, 0, 0, 255) 
	surface.SetTextPos( 100, 1116 )
	surface.DrawText( HP )
	--Blue Armor-
	surface.SetTextColor( 0, 0, 255, 255) 
	surface.SetTextPos( 100, 1146 )
	surface.DrawText( AR )
	-- HUD HP Slider
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
	surface.SetDrawColor( 255, 0, 0, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide  
	surface.SetDrawColor( 255, 117, 117, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade 
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
	-- HUD AR Slider
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
	surface.SetDrawColor( 0, 0, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
	surface.SetDrawColor( 117, 117, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
	-- HUD Alerts!
	surface.SetTextColor( 255, 0, 0, 255)														
	surface.SetTextPos( 200, 1116 )
	surface.SetFont( "Font1" )
	if HP <= 20 then surface.DrawText( "Heath Low!" ) end
	surface.SetTextColor( 0, 0, 255, 255)
	surface.SetTextPos( 200, 1146 )
	if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end										
	if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end 	

end
hook.Add("HUDPaint", "myhud", myhud)
local tohide = { -- HUD items to hide
	["CHudHealth"] = true,
	["CHudBattery"] = true,
	["CHudAmmo"] = true,
	["CHudSecondaryAmmo"] = true
}
local function HUDShouldDraw(name)
	if (tohide[name]) then     -- If the HUD name is a key in the table
		return false;      -- Return false.
	end 
end
hook.Add("HUDShouldDraw", "HUD hider", HUDShouldDraw)

Error
[ERROR] lua/crash_test.lua:2: unexpected symbol near ‘.’

  1. unknown - lua/crash_test.lua:0

You haven’t closed your CreateFont function.

You should learn the Lua syntaxis…

[lua]
surface.CreateFont( “FontULine”, {
font = “Arial”,
size = 20,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = true,

[/lua]

You are missing there a }) to close ( “FontULine”, {
(Please, don’t ask how to fix it…Just read the comment and TRY to solve it)


 surface.CreateFont( "FontULine", {
	font = "Arial",
	size = 20,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = true,
}) 
function myhud()
...
end

after closing got

[ERROR] lua/crash_test.lua:2: unexpected symbol near ‘.’

  1. unknown - lua/crash_test.lua:0

Test it, if this fails, continue experimenting with it until you reach a valid solution :v:

[ERROR] lua/crash_test.lua:2: unexpected symbol near ‘.’

  1. unknown - lua/crash_test.lua:0

I think that last line shouldn’t have a comma.

Post the full code again.

You’re also forgetting some mandatory checks… https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html

You’re missing Font1 too. I changed my normal _p for your client, but I did define _w as the weapon instead of repeating GetActiveWeapon. You also tried comparing an entity to a string ( camera ), so I added in GetClass for that comparison; if you’re referring to weapon_camera or gmod_camera then the name would need to be that or you’d need to use a string.find


surface.CreateFont( "FontULine", {
	font 		= "Arial",
	size 		= 20,
	weight 		= 500,
	blursize 	= 0,
	scanlines 	= 0,
	antialias 	= true,
	underline 	= false,
	italic 		= false,
	strikeout 	= false,
	symbol 		= false,
	rotary 		= false,
	shadow 		= false,
	additive 	= false,
	outline 	= true,
} );

surface.CreateFont( "Font1", {
	font 		= "Arial",
	size 		= 20,
	weight 		= 500,
	blursize 	= 0,
	scanlines 	= 0,
	antialias 	= true,
	underline 	= false,
	italic 		= false,
	strikeout 	= false,
	symbol 		= false,
	rotary 		= false,
	shadow 		= false,
	additive 	= false,
	outline 	= true,
} );

//
// Create a basic HUD which uses the player to draw stuff.. BASIC!
//
hook.Add("HUDPaint", "LuaHUDOurBasicHud", function( )
	// Init vars
	local client = LocalPlayer( );

	// Check if we should draw this hud
	local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" );

	// Mandatory Checks: If the player isn't valid, or we didn't want this to draw, then don't draw/return.
	if ( !IsValid( client ) || !_shoulddraw || !client:Alive( ) ) then return; end

	// Mandatory Weapon checks
	local _w = client:GetActiveWeapon( );
	if ( !IsValid( _w ) || _w:GetClass( ) == "camera" ) then return; end

	// Do HUD stuff...
	local HP = client:Health( ) --Hp Client has
	local AR = client:Armor( ) --Armor Client has
	-- HUD Base
	surface.SetDrawColor( 0, 0, 0, 120)
	surface.DrawRect( 90, ScrH() - 90, 250, 60) --Hud Back
	--Red HP- HUD Precents
	surface.SetFont( "FontULine" )
	surface.SetTextColor( 255, 0, 0, 255) 
	surface.SetTextPos( 100, 1116 )
	surface.DrawText( HP )
	--Blue Armor-
	surface.SetTextColor( 0, 0, 255, 255) 
	surface.SetTextPos( 100, 1146 )
	surface.DrawText( AR )
	-- HUD HP Slider
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawRect( 135, ScrH() - 85, 200, 20) -- HP Slide BackGround
	surface.SetDrawColor( 255, 0, 0, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 20) -- HP Slide  
	surface.SetDrawColor( 255, 117, 117, 255)
	surface.DrawRect( 135, ScrH() - 85, math.Clamp( HP, 0, 100)*2, 8) -- HP Slide shade 
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 85, 200, 20, Color( 255, 0, 0, 255 ) ) -- HP Outline
	-- HUD AR Slider
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawRect( 135, ScrH() - 55, 200, 20) --AR Slide BackGround
	surface.SetDrawColor( 0, 0, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 20) --AR Slide
	surface.SetDrawColor( 117, 117, 255, 255)
	surface.DrawRect( 135, ScrH() - 55, math.Clamp( AR, 0, 100)*2, 8) -- AR Slide Shade
	surface.SetDrawColor( 128, 128, 128, 255)
	surface.DrawOutlinedRect( 135, ScrH() - 55, 200, 20) --AR Outline
	-- HUD Alerts!
	surface.SetTextColor( 255, 0, 0, 255)														
	surface.SetTextPos( 200, 1116 )
	surface.SetFont( "Font1" )
	if HP <= 20 then surface.DrawText( "Heath Low!" ) end
	surface.SetTextColor( 0, 0, 255, 255)
	surface.SetTextPos( 200, 1146 )
	if AR >= 21 then surface.SetTextColor( 0, 0, 0, 0) end										
	if AR == 0 then surface.DrawText( "No Armor" ) else surface.DrawText( "Armor Low" ) end 	
end );

There is no errors but the text does not draw

[lua]
local _shoulddraw = hook.Call( “HUDShouldDraw”, GAMEMODE, “LuaHUDOurBasicHud” ) or true;
[/lua]

?

didn’t fix the draw problem dont know if it is not finding the font or just not drawing