How do I check where a player is on my screen?

I want to draw a red triangle around a player like this:

Though I am unsure on how to get the X,Y coordinates of the players.


Vector.ToScreen is very useful for such a thing. That would get roughly the x,y of his head.

Thanks for the reply, but when I draw the rectangle, it will ask for X and Y coordinates. What would I specify in there?

Like, could I use ply:EyePos():X() or something?

I apologize, I should have been more specific. You could do something like this.

[lua]local length = 40 – this is the side length in pixels of the square you darw
for _,pl in pairs(player.GetAll()) do
local pos = pl:EyePos():ToScreen();
surface.DrawRect(pos.x - length/2,pos.y - length/2,pos.x + length/2, pos.y + length/2);


Oh thanks, prior to reading that I was doing this:

[lua]if !( CLIENT ) then return end;

function DrawBoxes()
for k, v in pairs( player.GetAll() ) do
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawOutlinedRect( v:EyePos():ToScreen(), 40, 80 )
hook.Add( “HUDPaint”, “PlayerBoxes”, DrawBoxes )[/lua]

Wasn’t working out too well :[


Well I changed it a bit to this and it doesn’t seem to work at all.

[lua]function DrawBoxes()
local length = 40
for k, v in pairs ( player.GetAll() ) do
local PlayerPos = v:EyePos():ToScreen()
surface.SetDrawColor( Color( 255, 0, 0 ) )
surface.DrawRect(PlayerPos.x - length / 2, PlayerPos.y - length / 2, PlayerPos.x + length / 2, PlayerPos.y + length / 2 )

Got any ideas?


Oh, I forgot to hook it.



It works! After some adjustments I got exactly what I wanted, and I can start building from there :).

Only one problem, there’s a random box flying around at light speed and it’s definitely not a player. No idea what that is :stuck_out_tongue:

Check if the player is alive

It may also be players behind you, im now sure how ToScreen works

Also check that it’s not LocalPlayer

Oh that is probably the problem ^^

How do I check if it’s a localplayer or not?

[lua]for k, v in pairs ( player.GetAll() ) do
if not v == LocalPlayer() then


Thank you, also, random question:

How do I know if a player is alive?

I was looking at the wiki and I didn’t see an entry for Player:IsAlive()

I might’ve missed it though.

Its just Player:Alive()