Disabling hud elements as a function of distance

Hello, I am currently working on a hud that displays information about the user above their head. I have been able to get it to enable upon being within 250 units of a given player, but when that distance is reached with one player it activates the hud on ALL players. Here is the code,


function plyinfohud()

	local ply = LocalPlayer()

	for k , v in pairs(player.GetAll()) do

		if v ~= ply then 

			if v:GetPos():Distance(ply:GetPos()) < 250 then
		 	
		 		for _, ply in pairs(player.GetAll()) do

					local name = ply:Nick()
					local job = ply:getDarkRPVar("job")
					local teamcolor = team.GetColor(ply:Team())

					if ply ~= LocalPlayer() then

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

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

						local plyhudwidthn, plyhudheightn = surface.GetTextSize(name)
						local plyhudwidthj, plyhudheightj = surface.GetTextSize(job)

						if ply:Alive() then

							cam.Start3D2D(Pos, Ang, 0.1)


								if surface.GetTextSize(job) < surface.GetTextSize(name) then

									draw.RoundedBox( 10, ((plyhudwidthn/2)-plyhudwidthn)-5, 150, plyhudwidthn+10,100, Color( 0, 0, 0, 175 ) )
									draw.SimpleText(name,"plyhud",0,194,Color(255,255,255),1,1)
									draw.SimpleText(job,"plyhud",0,167,teamcolor,1,1)

								else
									
									draw.RoundedBox( 10, ((plyhudwidthj/2)-plyhudwidthj)-5, 150, plyhudwidthj+10,100, Color( 0, 0, 0, 175 ) )
									draw.SimpleText(name,"plyhud",0,194,Color(255,255,255),1,1)
									draw.SimpleText(job,"plyhud",0,167,teamcolor,1,1)
								
								end

						    		if ply:IsSuperAdmin() then
						    			
						    			surface.SetMaterial( superadmin )
						    			surface.SetDrawColor(255,255,255)
						    			surface.DrawTexturedRect(-16,210,32,32)

						    		else

						    			surface.SetMaterial( usergroup )
						    			surface.SetDrawColor(255,255,255)
						    			surface.DrawTexturedRect(-16,210,32,32)

						    		end
								
							cam.End3D2D()

						end

					end
						
				end

		 	end

		 end

	end

end

hook.Add("PostDrawOpaqueRenderables", "plyinfo", plyinfohud)

Here is a demonstration,

[video]https://youtu.be/2szOiRgTTPA[/video]

I am wondering how I can have it draw only the player this is within the 250 unit distance.

YES I know my code is messy, I am just learning lua and I plan on redoing and cleaning up the entire hud script once I finish all the aspects of the hud.

GM:PostPlayerDraw use this hook, without looping through all players ofc

Use it in place of PostDrawOpaqueRenderables?

Like this, instead of PostDrawOpaqueRenderables


hook.Add('PostPlayerDraw', 'plyinfohud', function(ply)
	if ply == LocalPlayer() and GetViewEntity() == LocalPlayer() then return end

	if ply:GetPos():Distance(LocalPlayer():GetPos()) < 250 and ply:Alive() then
		local name = ply:Nick()
		local job = ply:getDarkRPVar("job")
		local teamcolor = team.GetColor(ply:Team())

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

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

		local plyhudwidthn, plyhudheightn = surface.GetTextSize(name)
		local plyhudwidthj, plyhudheightj = surface.GetTextSize(job)

		cam.Start3D2D(Pos, Ang, 0.1)
			if surface.GetTextSize(job) < surface.GetTextSize(name) then
				draw.RoundedBox( 10, ((plyhudwidthn/2)-plyhudwidthn)-5, 150, plyhudwidthn+10,100, Color( 0, 0, 0, 175 ) )
				draw.SimpleText(name,"plyhud",0,194,Color(255,255,255),1,1)
				draw.SimpleText(job,"plyhud",0,167,teamcolor,1,1)
			else
				draw.RoundedBox( 10, ((plyhudwidthj/2)-plyhudwidthj)-5, 150, plyhudwidthj+10,100, Color( 0, 0, 0, 175 ) )
				draw.SimpleText(name,"plyhud",0,194,Color(255,255,255),1,1)
				draw.SimpleText(job,"plyhud",0,167,teamcolor,1,1)
			end

			if ply:IsSuperAdmin() then
				surface.SetMaterial( superadmin )
				surface.SetDrawColor(255,255,255)
				surface.DrawTexturedRect(-16,210,32,32)
			else
				surface.SetMaterial( usergroup )
				surface.SetDrawColor(255,255,255)
				surface.DrawTexturedRect(-16,210,32,32)
			end
		cam.End3D2D()
	end
end


Hi again Ralgoman, I just wanted to stop by and say sorry I didn’t post that 3D2D blur thing before and instead tried to convince you to try it in 2D when you wanted it to get smaller or bigger

I probably wasted a lot of time for you, so sorry :stuck_out_tongue:

Hey no worries man, only way for me to learn is to try new things. What 3d2d blur thing are you talking about? One that I can use in my cam? Or the video of the blur material?

Actually, I think I found another way that you may be able to use… good luck