Overhead hud help

I’m working on an over head hud, i’ve got everything figured out but there is a bug, you can take a look in the video.

[video]https://youtu.be/Pwe2qW1Rb5o[/video]

I’m not sure what is causing it to change length, here is my code.


function plyinfohud(ply)

	if ply == LocalPlayer() and GetViewEntity() == LocalPlayer() then return end
	if ply:GetPos():Distance(LocalPlayer():GetPos()) < 250 and ply:Alive() then

		local nick = ply:Nick()
		local nickWidth, nickHeight = surface.GetTextSize(nick)

		local job = ply:getDarkRPVar("job")
		if job == "Civil Protection" then
			job = "CP"
		elseif job == "Civil Protection Chief" then
			job = "CP Chief"
		end
		local teamcolor = team.GetColor(ply:Team())

		local HeadPos, HeadAng = ply:GetBonePosition( ply:LookupBone("ValveBiped.Bip01_Head1") )
		local Pos = HeadPos + Vector(0,0,20)

		local Ang = EyeAngles()
		Ang:RotateAroundAxis(Ang:Right(),90)
		Ang:RotateAroundAxis(Ang:Up(),-90)

		local x = -100
		local width = 200
		local height = 64
		local jobWidth, jobHeight = surface.GetTextSize(job)

		function main(x, y, w, h)
			surface.SetDrawColor(40,40,40)
			surface.DrawRect(x, y, w, h)

			surface.SetDrawColor(0,0,0)
			surface.DrawOutlinedRect(x, y, w, h)
		end

		function nickInfo(x, y, w, h)
			surface.SetDrawColor(60,60,60)
			surface.DrawRect(x, y, w, h)

			surface.SetDrawColor(0,0,0)
			surface.DrawOutlinedRect(x, y, w, h)
		end
		
		function jobInfo(x, y, w, h)
			surface.SetDrawColor(60,60,60)
			surface.DrawRect(x, y, w, h)

			surface.SetDrawColor(0,0,0)
			surface.DrawOutlinedRect(x, y, w, h)
		end
		print(nickWidth)
		cam.Start3D2D(Pos, Ang, 0.1)

			if nickWidth < 140 then
				main(-80, 0, 160, 64)
				jobInfo(-75, 34, 150, 24)
				nickInfo(-75, 5, 150, 24)
			elseif nickWidth < 170 then
				main(-95, 0, 190, 64)
				jobInfo(-90, 34, 180, 24)
				nickInfo(-90, 5, 180, 24)
			elseif nickWidth < 200 then
				main(-105, 0, 210, 64)
				jobInfo(-100, 34, 200, 24)
				nickInfo(-100, 5, 200, 24)
			elseif nickWidth < 230 then
				main(-115, 0, 230, 64)
				jobInfo(-110, 34, 220, 24)
				nickInfo(-110, 5, 220, 24)
			else
				main(-135, 0, 270, 64)
				jobInfo(-130, 34, 260, 24)
				nickInfo(-130, 5, 260, 24)
			end
			draw.SimpleText(job,"plyinfo",0,34,teamcolor,1,0)
			draw.DrawText(nick,"plyinfo",0,4,Color(255,255,255),1,0)

		cam.End3D2D()
	end
end

hook.Add("PostPlayerDraw", "plyinfohud", plyinfohud)


I would guess that the textsize is causing the issue but I wouldn’t know how to fix it… Any ideas?

[editline]9th February 2017[/editline]

Got rid of my if statements, recently just learned that you need to use surface.setfont before get the surface.gettextsize of a variable, solved the issue.