How can I make GetEyeTrace to work with a SWEP?

So, I’ve been trying to make the SWEP to do a specific function ONLY if the player is looking at an entity when it’s using the primary fire.
I tried this just for a test, but no luck. I’ve been searching all over the wiki’s withour luck aswell. Heres my basic code:
[lua]
function SWEP:PrimaryAttack()
if ply.GetEyeTrace().Entity == entity_test then
print(“Yup, it’s workin!”)
end
end
[/lua]
I’ll appreciate all the help I can get from all you wonderfull coders!
Thanks in advance,
Lolm4te.

It’s not
ply.GetEyeTrace()
It’s
ply:GetEyeTrace()

The second line:
[lua]if ply.GetEyeTrace().Entity == entity_test then[/lua]

Two things:
ply is not defined anywhere
ply.GetEyeTrace() won’t work the way you expect, it should be ply**:**GetEyeTrace()

Assuming entity_test is defined somewhere, this would be correct:
[lua]function SWEP:PrimaryAttack()
local ply = self.Owner
if not IsValid(ply) then return end --if the player is invalid (weapon has no owner) then we return and don’t proceed any further
if ply:GetEyeTrace().Entity == entity_test then
print( “Yup, it’s workin!” )
else
print( “Looking at”, ply:GetEyeTrace().Entity ) --added this to help debug
end
end[/lua]