Hud Fps goes down

Hello guys, please look at my hud code:


local hide = {
	CHudHealth = true,
	CHudBattery = true,
	CHudAmmo = true,	
	CHudSecondaryAmmo = true,
}

hook.Add( "HUDShouldDraw", "HideHUD", function( name )
	if ( hide[ name ] ) then return false end
end )

hook.Add( "HUDPaint", "DrawMyHud", function( )
	net.Receive( "Round_Status", function(len)
		RoundStatus=net.ReadInt(4)
	end)
	net.Receive( "Send_Countdown", function(len)
		Countdown=net.ReadInt(8)
	end)
	local BgHudC = 40
	local BgHudT = 220
	local HudC = 60
	local HudT = 200
	
	local health = LocalPlayer():Health()
	local armor = LocalPlayer():Armor()
	local weapon = LocalPlayer():GetActiveWeapon()

	local maxhealth = 200
	local maxarmor = 100

	local AvatarSize = 64
	local AvatarBgSize = 0

	//Stats
	local HpBarW = 200
	local HpBarH = 36
	local HpBarBgW = 0
	local HpBarBgH = 0

	local ArBarW = 100
	local ArBarH = 22
	local ArBarBgW = 0
	local ArBarBgH = 0

	//Weapon
	local AmBarW = 200
	local AmBarH = 36
	local AmBarBgW = 0
	local AmBarBgH = 0

	local ReBarW = 100
	local ReBarH = 22
	local ReBarBgW = 0
	local ReBarBgH = 0

	local Border = 2

	HpBarBgW=HpBarW+(2*Border)
	HpBarBgH=HpBarH+(2*Border)
	ArBarBgW=ArBarW+(2*Border)
	ArBarBgH=ArBarH+(2*Border)

	AmBarBgW=AmBarW+(2*Border)
	AmBarBgH=AmBarH+(2*Border)
	ReBarBgW=ReBarW+(2*Border)
	ReBarBgH=ReBarH+(2*Border)

	AvatarBgSize=AvatarSize+(2*Border)
	
	local versionL = 220
	local versionH = 29

	local status = " "

	draw.SimpleText( team.GetName( LocalPlayer():Team() ), "Tahoma 25",ScrW()/2, ScrH() * 0.92,Color(255,255,255),1,1 )

	local AvatarSize = 64
		
	draw.RoundedBox(4,(ScrW()/2)-((AvatarBgSize)/2), ScrH()-(AvatarBgSize+Border), AvatarBgSize, AvatarBgSize, Color(BgHudC,BgHudC,BgHudC,BgHudT))
	local Avatar = vgui.Create( "AvatarImage", Panel )
	Avatar:SetSize( AvatarSize, AvatarSize )
	Avatar:SetPos( (ScrW()/2)-(AvatarSize/2), ScrH()-(AvatarSize+(2*Border)) )
	Avatar:SetPlayer( LocalPlayer(), AvatarSize )

	if(RoundStatus == 0)then
		if(team.NumPlayers( 3 )==1)then
			status = "Waiting for " .. 2-team.NumPlayers( 3 ) .. " more players!"
		elseif(team.NumPlayers( 3 )==0)then
			status = "Waiting for " .. 2-team.NumPlayers( 3 ) .. " more players!"
		end
	end
	if(RoundStatus == 1)then
		status = Countdown .. " sec until Starts"
	end
	if(RoundStatus == 2)then
		if(ClientDead == 1)then
			status = team.TotalDeaths( 1 ) .. " dead."
			timer.Create( "ClientStatusTimer", 4, 1, function()
				ClientDead = 0
			end)
		end
	end
	draw.SimpleText( status, "Tahoma 40",ScrW() *0.5, ScrH() *0.1,Color(255,255,255),1,1 )

	if(LocalPlayer():Team()==1)then
		if health > 0 then
			draw.RoundedBox(4,(ScrW()/2)-(HpBarBgW+(AvatarBgSize/2)+Border)-4, ScrH()-(HpBarBgH+Border), HpBarBgW+4, HpBarBgH,Color(BgHudC,BgHudC,BgHudC,BgHudT))
			draw.RoundedBox(4,(ScrW()/2)-(HpBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(HpBarH+(2*Border)), HpBarW+4, HpBarH,Color(HudC,HudC,HudC,HudT))
			draw.RoundedBox(4,(ScrW()/2)-(HpBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(HpBarH+(2*Border)), health*(HpBarW/maxhealth)+4, HpBarH, Color( (health-maxhealth)*-(255/maxhealth), health*(255/maxhealth), 0 ))
			draw.SimpleText(health.. "/" .. maxhealth .. " Health","Tahoma 25",(ScrW()/2)-((HpBarW/2)+(AvatarBgSize/2)+(2*Border)), ScrH()-((HpBarH/2)+(2*Border)), Color( 255,255,255 ), 1, 1)
			
			draw.RoundedBox(4,(ScrW()/2)-(ArBarBgW+(AvatarBgSize/2)+Border)-4, ScrH()-(ArBarBgH+HpBarBgH+(2*Border)), ArBarBgW+4, ArBarBgH, Color(BgHudC,BgHudC,BgHudC,BgHudT))
			draw.RoundedBox(4,(ScrW()/2)-(ArBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(ArBarBgH+HpBarBgH+(1*Border)), ArBarW+4, ArBarH,Color(HudC,HudC,HudC,HudT))
			draw.RoundedBox(4,(ScrW()/2)-(ArBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(ArBarBgH+HpBarBgH+(1*Border)), armor*(ArBarW/maxarmor)+4, ArBarH, Color((armor-100)*-(255/maxarmor),(armor-100)*-(255/maxarmor),255))
			draw.SimpleText(armor .. "/100 Armor","Tahoma 16",(ScrW()/2)-((ArBarW/2)+(AvatarBgSize/2)+(2*Border)), ScrH()-((ArBarBgH/2)+HpBarBgH+(2*Border)),Color(255,255,255),1,1)
              end
	end

	//Spectator View
	if(LocalPlayer():Team()==2)then
		draw.RoundedBox( 0, 0, 0, ScrW(), 60, Color(0,0,0,240) )
		draw.RoundedBox( 0, 0, ScrH()-60, ScrW(), 60, Color(0,0,0,240) )
		draw.SimpleText("Spectating","Tahoma 25", ScrW()*0.5, 44,Color(255,255,255),1,1)
	end
end)

console says
cl_hud.lua:97: attempt to concatenate global ‘Countdown’ (a nil value)

ingame:
240fps at start

after 10 sec
60fps

after 30sec
30fps

goes down to 20 fps

net.Receive shouldn’t be in the HUDPaint hook (hence fps drop) and the variables ‘Countdown’ and ‘RoundStatus’ are only in the scope of their net receive functions which is why Countdown is undefined



local hide = {
	CHudHealth = true,
	CHudBattery = true,
	CHudAmmo = true,	
	CHudSecondaryAmmo = true,
}

hook.Add( "HUDShouldDraw", "HideHUD", function( name )
	if ( hide[ name ] ) then return false end
end )

local Countdown = 0
local RoundStatus = 0

net.Receive( "Round_Status", function(len)
	RoundStatus=net.ReadInt(4)
end)
net.Receive( "Send_Countdown", function(len)
		Countdown=net.ReadInt(8)
end)

hook.Add( "HUDPaint", "DrawMyHud", function( )

	local BgHudC = 40
	local BgHudT = 220
	local HudC = 60
	local HudT = 200
	
	local health = LocalPlayer():Health()
	local armor = LocalPlayer():Armor()
	local weapon = LocalPlayer():GetActiveWeapon()

	local maxhealth = 200
	local maxarmor = 100

	local AvatarSize = 64
	local AvatarBgSize = 0

	//Stats
	local HpBarW = 200
	local HpBarH = 36
	local HpBarBgW = 0
	local HpBarBgH = 0

	local ArBarW = 100
	local ArBarH = 22
	local ArBarBgW = 0
	local ArBarBgH = 0

	//Weapon
	local AmBarW = 200
	local AmBarH = 36
	local AmBarBgW = 0
	local AmBarBgH = 0

	local ReBarW = 100
	local ReBarH = 22
	local ReBarBgW = 0
	local ReBarBgH = 0

	local Border = 2

	HpBarBgW=HpBarW+(2*Border)
	HpBarBgH=HpBarH+(2*Border)
	ArBarBgW=ArBarW+(2*Border)
	ArBarBgH=ArBarH+(2*Border)

	AmBarBgW=AmBarW+(2*Border)
	AmBarBgH=AmBarH+(2*Border)
	ReBarBgW=ReBarW+(2*Border)
	ReBarBgH=ReBarH+(2*Border)

	AvatarBgSize=AvatarSize+(2*Border)
	
	local versionL = 220
	local versionH = 29

	local status = " "

	draw.SimpleText( team.GetName( LocalPlayer():Team() ), "Tahoma 25",ScrW()/2, ScrH() * 0.92,Color(255,255,255),1,1 )

	local AvatarSize = 64
		
	draw.RoundedBox(4,(ScrW()/2)-((AvatarBgSize)/2), ScrH()-(AvatarBgSize+Border), AvatarBgSize, AvatarBgSize, Color(BgHudC,BgHudC,BgHudC,BgHudT))
	local Avatar = vgui.Create( "AvatarImage", Panel )
	Avatar:SetSize( AvatarSize, AvatarSize )
	Avatar:SetPos( (ScrW()/2)-(AvatarSize/2), ScrH()-(AvatarSize+(2*Border)) )
	Avatar:SetPlayer( LocalPlayer(), AvatarSize )

	if(RoundStatus == 0)then
		if(team.NumPlayers( 3 )==1)then
			status = "Waiting for " .. 2-team.NumPlayers( 3 ) .. " more players!"
		elseif(team.NumPlayers( 3 )==0)then
			status = "Waiting for " .. 2-team.NumPlayers( 3 ) .. " more players!"
		end
	end
	if(RoundStatus == 1)then
		status = Countdown .. " sec until Starts"
	end
	if(RoundStatus == 2)then
		if(ClientDead == 1)then
			status = team.TotalDeaths( 1 ) .. " dead."
			timer.Create( "ClientStatusTimer", 4, 1, function()
				ClientDead = 0
			end)
		end
	end
	draw.SimpleText( status, "Tahoma 40",ScrW() *0.5, ScrH() *0.1,Color(255,255,255),1,1 )

	if(LocalPlayer():Team()==1)then
		if health > 0 then
			draw.RoundedBox(4,(ScrW()/2)-(HpBarBgW+(AvatarBgSize/2)+Border)-4, ScrH()-(HpBarBgH+Border), HpBarBgW+4, HpBarBgH,Color(BgHudC,BgHudC,BgHudC,BgHudT))
			draw.RoundedBox(4,(ScrW()/2)-(HpBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(HpBarH+(2*Border)), HpBarW+4, HpBarH,Color(HudC,HudC,HudC,HudT))
			draw.RoundedBox(4,(ScrW()/2)-(HpBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(HpBarH+(2*Border)), health*(HpBarW/maxhealth)+4, HpBarH, Color( (health-maxhealth)*-(255/maxhealth), health*(255/maxhealth), 0 ))
			draw.SimpleText(health.. "/" .. maxhealth .. " Health","Tahoma 25",(ScrW()/2)-((HpBarW/2)+(AvatarBgSize/2)+(2*Border)), ScrH()-((HpBarH/2)+(2*Border)), Color( 255,255,255 ), 1, 1)
			
			draw.RoundedBox(4,(ScrW()/2)-(ArBarBgW+(AvatarBgSize/2)+Border)-4, ScrH()-(ArBarBgH+HpBarBgH+(2*Border)), ArBarBgW+4, ArBarBgH, Color(BgHudC,BgHudC,BgHudC,BgHudT))
			draw.RoundedBox(4,(ScrW()/2)-(ArBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(ArBarBgH+HpBarBgH+(1*Border)), ArBarW+4, ArBarH,Color(HudC,HudC,HudC,HudT))
			draw.RoundedBox(4,(ScrW()/2)-(ArBarW+(AvatarBgSize/2)+(2*Border))-4, ScrH()-(ArBarBgH+HpBarBgH+(1*Border)), armor*(ArBarW/maxarmor)+4, ArBarH, Color((armor-100)*-(255/maxarmor),(armor-100)*-(255/maxarmor),255))
			draw.SimpleText(armor .. "/100 Armor","Tahoma 16",(ScrW()/2)-((ArBarW/2)+(AvatarBgSize/2)+(2*Border)), ScrH()-((ArBarBgH/2)+HpBarBgH+(2*Border)),Color(255,255,255),1,1)
              end
	end

	//Spectator View
	if(LocalPlayer():Team()==2)then
		draw.RoundedBox( 0, 0, 0, ScrW(), 60, Color(0,0,0,240) )
		draw.RoundedBox( 0, 0, ScrH()-60, ScrW(), 60, Color(0,0,0,240) )
		draw.SimpleText("Spectating","Tahoma 25", ScrW()*0.5, 44,Color(255,255,255),1,1)
	end
end)


Thanks for the fast REPLY! :slight_smile:

[editline]31st December 2015[/editline]

ok i changed it but i get 35fps at the end
i have gtx780 and quadcore normally i have 120fps in gmod in other gamemodes
what is wrong now?
when i put the cl_hud.lua away i have 120fps on the server but with it 35fps

[editline]31st December 2015[/editline]


local Countdown = 0
local RoundStatus = 0

net.Receive( "Round_Status", function(len)
	RoundStatus=net.ReadInt(4)
end)
net.Receive( "Send_Countdown", function(len)
		Countdown=net.ReadInt(8)
end)

this was added right?

and net.receives both are away from painthud

[editline]31st December 2015[/editline]

This is the code that make the fps goes down i comment out and this is the result:


	--draw.SimpleText( team.GetName( LocalPlayer():Team() ), "Tahoma 25",ScrW()/2, ScrH() * 0.92,Color(255,255,255),1,1 )
--local Avatar = vgui.Create( "AvatarImage", Panel )
	--Avatar:SetSize( AvatarSize, AvatarSize )
	--Avatar:SetPos( (ScrW()/2)-(AvatarSize/2), ScrH()-(AvatarSize+(2*Border)) )
	--Avatar:SetPlayer( LocalPlayer(), AvatarSize )

	--if(RoundStatus == 0)then
	--	if(team.NumPlayers( 3 )==1)then
	--		status = "Waiting for " .. 2-team.NumPlayers( 3 )
	--	elseif(team.NumPlayers( 3 )==0)then
	--		status = "Waiting for " .. 2-team.NumPlayers( 3 )
	--	end
	--end
	--if(RoundStatus == 1)then
	--	status = Countdown .. " sec until Starts"
	--end
	--if(RoundStatus == 2)then
	--	if(ClientDead == 1)then
	--		status = team.TotalDeaths( 1 ) .. " dead. " ..  team.NumPlayers( 1 ) .. " alive."
	--		timer.Create( "ClientStatusTimer", 4, 1, function()
	--			ClientDead = 0
	--		end)
	--	end
	--end


take the avatar out the HUD.Paint() hook, you’re drawing every frame!

is there a way to remove it? after it draw it once

[editline]31st December 2015[/editline]

i mean when it was outside of hud.paint()

Just draw it out of HUD Paint? I don’t quite understand what you’re saying

A vgui is an element and don’t something you call in HUDPaint.
You are creating a new AvatarImage every frame when its inside HUDPaint.

1 seccond = 30 AvatarImages on the screen to draw
30 seconds = 900 AvatarImages on the screen to draw
1 min = 1800 AvatarImages to draw.
…ect

It is a comment problem for beginners and there are many examples and solutions, if you google it:
http://forum.facepunch.com/showthread.php?t=1272284

Draw the avatar outside of the hook, and if needed use PaintManual, and if you do such with other VGUI elements, like the PlyModel, make an function to update it with the model on spawn.

Not that it really affects FPS too much, but you’re recreating tons of variables every time the hud is drawn. Things like “maxhealth” only need to be stated once, and you can do so outside of and above the HUDPaint hook.

no i mean when i spawn the avatarimage, i cant get it away after it it is there atleast i quit the server