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

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

http://img594.imageshack.us/img594/2184/capturekhn.png

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


ply:EyePos():ToScreen()

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
hook.Add(“HUDPaint”,“DrawPlayerBoxes”,function()
for _,pl in pairs(player.GetAll()) do
local pos = pl:EyePos():ToScreen();
surface.SetDrawColor(Color(255,0,0));
surface.DrawRect(pos.x - length/2,pos.y - length/2,pos.x + length/2, pos.y + length/2);
end
end)[/lua]

**[Vector.ToScreen

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Vector.ToScreen)**

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 )
end
end
hook.Add( “HUDPaint”, “PlayerBoxes”, DrawBoxes )[/lua]

Wasn’t working out too well :[

[editline]03:32AM[/editline]

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 )
end
end
[/lua]

Got any ideas?

[editline]03:32AM[/editline]

Oh, I forgot to hook it.

|:

[editline]03:41AM[/editline]

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

end
end
[/lua]

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()