Recognizing an Ent

Hey guys. Well i know how to do HUD and everything else but what i need help with is having it recognize if the player is looking at the entity “treasure”.

[lua]
local pos = LocalPlayer():GetShootPos()
local ang = LocalPlayer():GetAimVector()
local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+(ang*150)
tracedata.filter = LocalPlayer()
local trace = util.TraceLine(tracedata)
[/lua]

That is the code i use to get what the player is looking at. Again, what I’m trying to figure out is how to make it recognize if it is looking at the entity “treasure”.

Thanks =)

‘treasure’ would be the classname or the name of the entity?

[lua]
if( trace.Entity:GetClass( ) == “treasure” ) then

-- ...

end

if( trace.Entity:GetName( ) == “treasure” ) then

-- ...

end
[/lua]

Name of the entity.

Thanks a ton Nevec.

Edit:
Im getting this error?

Hook ‘drawthehud’ Failed: GoFish/gamemode/cl_init.lua:86: attempt to call method ‘GetName’ (a nil value)

Then remove lines 07, 08, 09, 10, 11 out of nevecs code.

I knew that :stuck_out_tongue:

I tried both, and returned the same error only with couldnt call method GetClass.

[lua]
local pos = LocalPlayer():GetShootPos()
local ang = LocalPlayer():GetAimVector()
local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+(ang*150)
tracedata.filter = LocalPlayer()
local trace = util.TraceLine(tracedata)
if ValidEntity(trace.Entity) then
if trace.Entity:GetClass() == “myent” then
– Do awesome crap here
end
end
[/lua]

:eng101:

Veryyyy strange! I had code pretty similar to that and it didnt work.

Your code worked perfectly
Thanks!