PlayerEyeTrace hit a certain entity

Can someone give me an example for when a player’s eyes hit a certain entity?

What? Can you be more descriptive?

I am coding an entity so when a player looks at it, it will explode.

[lua]
if ply:GetEyeTrace.Entity == ent then

--he looks!

end

[/lua]

This is a very dumb question, I am aware. What function would I put said example for the entity?

Preferably one that is called when a player starts looking at en entity, unfortunately such one doesn’t exist. Clearly then it’d make sense to just constantly check if a player is looking at an entity, and if they are then explode it.

Here is a list of entity hooks or rather, events:
http://gmodwiki.net/Lua/Events/Entity

Should be fairly obvious which one I am talking about, I just figured explaining the thought process is more helpful than giving you a simple answer.

I was thinking Initialize or Think but neither of them work.


 ENT.Type = "anim"
ENT.PrintName			= "Explosive Slam"
ENT.Author			= ""
ENT.Contact			= ""
ENT.Purpose			= ""
ENT.Instructions			= ""

if SERVER then

AddCSLuaFile( "shared.lua" )

function ENT:Initialize( ply )

	self.Owner = self.Entity.Owner

	self.Entity:SetModel("models/weapons/w_slam.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:DrawShadow( false )
	self.Entity:SetCollisionGroup( COLLISION_GROUP_WEAPON )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
	phys:Wake()
	end

	if ply:GetEyeTrace.Entity == "ttt_slamemine" then
self:Explosion()
end


and with the Think function


ENT.Type = "anim"
ENT.PrintName			= "Explosive Slam"
ENT.Author			= ""
ENT.Contact			= ""
ENT.Purpose			= ""
ENT.Instructions			= ""

if SERVER then

AddCSLuaFile( "shared.lua" )

function ENT:Initialize( ply )

	self.Owner = self.Entity.Owner

	self.Entity:SetModel("models/weapons/w_slam.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:DrawShadow( false )
	self.Entity:SetCollisionGroup( COLLISION_GROUP_WEAPON )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
	phys:Wake()
	end

end

function ENT:Think(ply)
	if ply:GetEyeTrace.Entity == "ttt_slammine" then
self:Explosion()
end


I’m using the slam model as a place holder.

Initialize only gets called once, why would that help for checking something continously?
As for your 2nd piece of code your syntax is wrong as you’re missing an “end” at the Think function.
Did you even define the “Explosion” function?

1: I’ve been up since 4AM, didn’t think about that.

2: No errors came back when I saved it. Refreshed the map and no errors

3: Yes I did, I didn’t include it sorry.

Thanks for the help guys. I also tested it with print(“test”) and nothing outputted.



function ENT:Explosion()

	local explosion = ents.Create("env_explosion")
		explosion:SetOrigin(self.Entity:GetPos())
		explosion:SetEntity(self.Entity)
		explosion:SetScale(1)	
		explosion:SetRadius(67)
		explosion:SetMagnitude(18)


	
	self.Entity:EmitSound("ambient/explosions/explode_" .. math.random(1, 4) .. ".wav", self.Entity:GetPos(), 100, 100 )
	local scorchstart = self.Entity:GetPos() + ((Vector(0,0,1)) * 5)
	local scorchend = self.Entity:GetPos() + ((Vector(0,0,-1)) * 5)
	self.Entity:Remove()
	util.Decal("Scorch", scorchstart, scorchend)
	
end