Using if statements with GetEyeTrace.Entity?

I wanna use an if statement so my SWEP only works with a certain entity. I have tried:

if self.Owner:GetEyeTrace().Entity == "gm_whatever" then dostuffhere end

But doesnt work. Neither does it tell me of any errors.

The entity isn’t a string (it’s an entity or NULL). First check if the entity is valid with, and then use to get the entity’s classname to use in your if-statement.

/shared.lua:108: attempt to call global ‘ValidEntity’ (a nil value)

IS what I get :c

function SWEP:PrimaryAttack()
	if CLIENT then  end

	if SERVER then
	if ValidEntity(self.Owner:GetEyeTrace().Entity) then end

It should be
[lua]if CLIENT then return end[/lua] otherwise it won’t do anything. The return makes it end the function, skipping everything onwards.
If you were getting clientside errors that would be why.

[editline]26th October 2013[/editline]

Also, it should be
[lua]if !self.Owner:GetEyeTrace().Entity:IsValid() then return end[/lua]
if you don’t want it to run with an invalid entity.

ValidEntity was changed to IsValid upon Gmod13, if I recall.

Tip, with traces, sometimes players return as nil for no apparent reason.
At least in my experience.

Probably not with eye traces though.