How do I get enitys's position?

I need to get the coordinate position of an entity which is on the map. Somebody told me to write “getpos” in the console but it seems to get the current player’s position instead of entity’s one. How do you do this?

Sorry, I’m bad at this. But where should I paste this code?

How are you wanting to get the entity’s position? In your code or just by a console command?

Better by a console command. I need to get the position of the entity I’m looking at.

Player:GetEyeTrace()

Use that to check if the entity’s your looking at is the class you want.

Since all you seem to want is to get the position of an entity on the map here is a chat command that will do that. Place this in any server autorun section and when in game simply type /getpos in chat while looking at the entity you want the position for. The position and the entity name you are looking at will both be printed to the console.

[LUA]
hook.Add( “PlayerSay”, “getPos”, function( ply, text)
if !text == “/getpos” then return end
local tr = ply:GetEyeTrace()
if !tr.Entity:IsValid() then return end
ply:PrintMessage(2, "You are looking at " … tr.Entity:GetClass())
ply:PrintMessage(2, "The entities position is " … tostring(tr.Entity:GetPos()))
end)
[/LUA]

Alternatively here is a console command that will do the same thing.

[LUA]
concommand.Add( “entPos”, function( ply )
local tr = ply:GetEyeTrace()
if !tr.Entity:IsValid() then return end
ply:PrintMessage(2, "You are looking at " … tr.Entity:GetClass())
ply:PrintMessage(2, "The entities position is " … tostring(tr.Entity:GetPos()))
end )
[/LUA]

Very nice. Thanks a lot. That helped me.