Player in distance help.

So I want my cam3D2D to only show up when a player in in the set distance.
My current code is not working and I don’t know why. Please help.

Current Code:


local ply = LocalPlayer()
local DrawDistance = 250
local dist = ply:GetPos():Distance( LocalPlayer():GetShootPos() )

function DrawName( ply )
	local eye = LocalPlayer():GetEyeTrace()
	if !ply:Alive() then return end
 
	local offset = Vector( 0, 0, 85 )
	local ang = LocalPlayer():EyeAngles()
	local pos = ply:GetPos() + offset + ang:Up()
 
	ang:RotateAroundAxis( ang:Forward(), 90 )
	ang:RotateAroundAxis( ang:Right(), 90 )
 
 	if dist then
	cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )
		// Base
		draw.RoundedBox(0,2,2,200,100,Color(50,50,50,255), TEXT_ALIGN_CENTER)
		// Title Stuff
		draw.RoundedBox(0,5,5,194,15,backgroundColorTable[backgroundColor])
		draw.DrawText(ply:GetName(), "EntityDisplayFont", 104, 3, Color(255,255,255,255), TEXT_ALIGN_CENTER)
		// Job Stuff
		draw.RoundedBox(0,9,27,50,17,backgroundColorTable[backgroundColor])
		draw.DrawText("Job", "EntityDisplayFont", 34, 26, Color(255,255,255,255), TEXT_ALIGN_CENTER)
		draw.DrawText(ply:getDarkRPVar("job"), "EntityDisplayFont", 62, 26, Color(255,255,255,255))
		// Money Stuff 
		local function formatNumber(n)
			if not n then return "" end
			if n >= 1e14 then return tostring(n) end
			n = tostring(n)
			local sep = sep or ","
			local dp = string.find(n, "%.") or #n+1
			for i=dp-4, 1, -3 do
				n = n:sub(1, i) .. sep .. n:sub(i+1)
			end
			return n
		end

		local DrawWallet = "$"..formatNumber(ply:getDarkRPVar("money") or 0)
		draw.RoundedBox(0,9,47,50,17,backgroundColorTable[backgroundColor])
		draw.DrawText("Money", "EntityDisplayFont", 34, 46, Color(255,255,255,255), TEXT_ALIGN_CENTER)
		draw.DrawText(DrawWallet, "EntityDisplayFont", 63, 46, Color(255,255,255,255))
		// Health Stuff
		draw.RoundedBox(0, 9, 75, 186, 10, Color(105,105,105,255))
		local DrawHealth = ply:Health() or ""
		local EchoHealth = ply:Health() or ""
		if DrawHealth > 100 then DrawHealth = 100 end
		if DrawHealth < 0 then DrawHealth = 0 end

		if DrawHealth != 0 then
			draw.RoundedBox(0, 9, 75, 186 * DrawHealth / 100, 10, Color(178,34,34,255))
		end	
		draw.SimpleText("Health: "..EchoHealth ,"EntityDisplayFont1", 100, 74, Color(255,255,255,255), TEXT_ALIGN_CENTER)
		// Armor Stuff
		draw.RoundedBox(0, 9, 86, 186, 10, Color(105,105,105,255))
		local DrawArmor = ply:Armor() or ""
		local EchoArmor = ply:Armor() or ""
		if DrawArmor > 100 then DrawArmor = 100 end
		if DrawArmor < 0 then DrawArmor = 0 end

		if DrawHealth != 0 then
			draw.RoundedBox(0, 9, 86, 186 * DrawArmor / 100, 10, Color(0,141,223,255))
		end
		draw.SimpleText("Armor: "..EchoArmor, "EntityDisplayFont1", 100, 84, Color(255,255,255,255), TEXT_ALIGN_CENTER)
		// Has License
		if ply:getDarkRPVar("HasGunlicense") then
			surface.SetMaterial(Material("icon16/page.png"))
			surface.SetDrawColor(255,255,255,255)
			surface.DrawTexturedRect(6,6,12,12)
		end
		// If Wanted
		if ply:isWanted() then
			surface.SetMaterial(Material("icon16/star.png"))
			surface.SetDrawColor(255,255,255,255)
			surface.DrawTexturedRect(185,6,12,12)
		end
		// Borders
		draw.RoundedBox(0,3,3,2,98,Color(105,105,105,255), TEXT_ALIGN_CENTER)
		draw.RoundedBox(0,3,3,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
		draw.RoundedBox(0,3,20,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
		draw.RoundedBox(0,199,3,2,98,Color(105,105,105,255), TEXT_ALIGN_CENTER)
		draw.RoundedBox(0,3,99,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
	cam.End3D2D()
	end
end
hook.Add( "PostPlayerDraw", "DrawName", DrawName )

There’s quite a lot of problems with your code.

The first one is that you’re setting ply to LocalPlayer(), but then setting the DrawName function’s argument to also be called ply. Which ply are you even referring to?

The second one is all the variables should be inside the function (especially the dist one since that needs to be updated every frame), and the dist variable should measure the distance from the local player to the other player, not their shoot position.

The third problem is that you made a variable called DrawDistance but you never even used it. The code otherwise seems well-structured enough but these obvious problems make it seem like you copied it from somewhere, surely you would’ve noticed one of these if you wrote this yourself?

[editline]16th January 2017[/editline]

Also, the dist check should be



if dist < DrawDistance then


[editline]16th January 2017[/editline]

Also, why are you even doing



local ply = LocalPlayer()


If you’re using LocalPlayer() multiple times in the DrawName function anyway? It won’t make a performance difference, and LocalPlayer() may not even be initialized at the time of you setting the variable

[editline]16th January 2017[/editline]

Also, you’re not using the eyetrace variable ‘eye’ anywhere

So What i want to do is the cam3D2D to show when the LocalPlayer gets close to the other player.



if dist < DrawDistance then


Does not work

Well as I mentioned before there’s about 99 other problems that will have to be fixed as well as that one if you expect any of it to work at all

Ok few questions:

  1. Does this code look any better?

local DrawDistance = 250
local dist = LocalPlayer():GetPos():Distance( LocalPlayer():GetShootPos() )

function DrawName( ply, dist )
	local eye = LocalPlayer():GetEyeTrace()
	if !ply:Alive() then return end
 
	local offset = Vector( 0, 0, 85 )
	local ang = LocalPlayer():EyeAngles()
	local pos = ply:GetPos() + offset + ang:Up()
 
	ang:RotateAroundAxis( ang:Forward(), 90 )
	ang:RotateAroundAxis( ang:Right(), 90 )
 
 	if LocalPlayer():IsLineOfSightClear( ply ) then
	cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )


This is only half the actual code

  1. How would I put the DrawDistance into the coed

Yes, a fried let me use the code

If a friend let you use it then why don’t you ask the friend to get it to work properly?

Anyway, I’m trying to fix some stuff with it but it’ll take a while

The friend is not online, he is on vacation.

I changed a few things with the code, not sure if it works yet, I’ll test it now



local function formatNumber(n)
	if not n then return "" end
	if n >= 1e14 then return tostring(n) end
	n = tostring(n)
	local sep = sep or ","
	local dp = string.find(n, "%.") or #n+1
	for i=dp-4, 1, -3 do
		n = n:sub(1, i) .. sep .. n:sub(i+1)
	end
	return n
end

local function DrawName( ply )

	if !ply:Alive() then return end
 
	local offset = Vector( 0, 0, 85 )
	local ang = LocalPlayer():EyeAngles()
	local pos = ply:GetPos() + offset + ang:Up()
 
	ang:RotateAroundAxis( ang:Forward(), 90 )
	ang:RotateAroundAxis( ang:Right(), 90 )

 	local dist = ply:GetPos():Distance( LocalPlayer():GetShootPos() )
 	if dist < 250 then
		cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )
			// Base
			draw.RoundedBox(0,2,2,200,100,Color(50,50,50,255), TEXT_ALIGN_CENTER)
			// Title Stuff
			draw.RoundedBox(0,5,5,194,15,backgroundColorTable[backgroundColor])
			draw.DrawText(ply:GetName(), "EntityDisplayFont", 104, 3, Color(255,255,255,255), TEXT_ALIGN_CENTER)
			// Job Stuff
			draw.RoundedBox(0,9,27,50,17,backgroundColorTable[backgroundColor])
			draw.DrawText("Job", "EntityDisplayFont", 34, 26, Color(255,255,255,255), TEXT_ALIGN_CENTER)
			draw.DrawText(ply:getDarkRPVar("job"), "EntityDisplayFont", 62, 26, Color(255,255,255,255))
			// Money Stuff 
			local DrawWallet = "$"..formatNumber(ply:getDarkRPVar("money") or 0)
			draw.RoundedBox(0,9,47,50,17,backgroundColorTable[backgroundColor])
			draw.DrawText("Money", "EntityDisplayFont", 34, 46, Color(255,255,255,255), TEXT_ALIGN_CENTER)
			draw.DrawText(DrawWallet, "EntityDisplayFont", 63, 46, Color(255,255,255,255))
			// Health Stuff
			draw.RoundedBox(0, 9, 75, 186, 10, Color(105,105,105,255))
			local DrawHealth = ply:Health() or ""
			local EchoHealth = ply:Health() or ""
			if DrawHealth > 100 then DrawHealth = 100 end
			if DrawHealth < 0 then DrawHealth = 0 end

			if DrawHealth != 0 then
				draw.RoundedBox(0, 9, 75, 186 * DrawHealth / 100, 10, Color(178,34,34,255))
			end	
			draw.SimpleText("Health: "..EchoHealth ,"EntityDisplayFont1", 100, 74, Color(255,255,255,255), TEXT_ALIGN_CENTER)
			// Armor Stuff
			draw.RoundedBox(0, 9, 86, 186, 10, Color(105,105,105,255))
			local DrawArmor = ply:Armor() or ""
			local EchoArmor = ply:Armor() or ""
			if DrawArmor > 100 then DrawArmor = 100 end
			if DrawArmor < 0 then DrawArmor = 0 end

			if DrawHealth != 0 then
				draw.RoundedBox(0, 9, 86, 186 * DrawArmor / 100, 10, Color(0,141,223,255))
			end
			draw.SimpleText("Armor: "..EchoArmor, "EntityDisplayFont1", 100, 84, Color(255,255,255,255), TEXT_ALIGN_CENTER)
			// Has License
			if ply:getDarkRPVar("HasGunlicense") then
				surface.SetMaterial(Material("icon16/page.png"))
				surface.SetDrawColor(255,255,255,255)
				surface.DrawTexturedRect(6,6,12,12)
			end
			// If Wanted
			if ply:isWanted() then
				surface.SetMaterial(Material("icon16/star.png"))
				surface.SetDrawColor(255,255,255,255)
				surface.DrawTexturedRect(185,6,12,12)
			end
			// Borders
			draw.RoundedBox(0,3,3,2,98,Color(105,105,105,255), TEXT_ALIGN_CENTER)
			draw.RoundedBox(0,3,3,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
			draw.RoundedBox(0,3,20,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
			draw.RoundedBox(0,199,3,2,98,Color(105,105,105,255), TEXT_ALIGN_CENTER)
			draw.RoundedBox(0,3,99,197,2,Color(105,105,105,255), TEXT_ALIGN_CENTER)
		cam.End3D2D()
	end
end

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


IT WORKS GREAT! THANK YOU SO MUCH!

P.S. Huge fan of Death Animations. Great Job on it

The distance check I post before seems to work now, try testing it on a bot to see if it works for you

[editline]16th January 2017[/editline]

that’s okay lol