lua distance

Ok, so I’m working on a taser addon and trying to restrict the taser to only work if the target is inside a certain radius of the shooter. So here is what I have tried so far:


function SWEP:PrimaryAttack() if ( !self:CanPrimaryAttack() ) then return end
 local eyetrace = self.Owner:GetEyeTrace(); 
 if !eyetrace.Entity:IsPlayer() then 
  if !eyetrace.Entity:IsNPC() then return end       // Check to see if what the player is aiming at is an NPC or Player
  end
  
self.Weapon:EmitSound( "Weapon_StunStick.Activate")  
self.BaseClass.ShootEffects( self ) 
self:TakePrimaryAmmo(1) 
self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)


 
 if (!SERVER) then return end 
 
 if eyetrace.Entity:IsPlayer() and
 math.Dist(self:GetPos(), eyetrace:GetPos()) > 50 then
 self.Owner:PrintMessage( HUD_PRINTCENTER, "Now right click to electrocute "..eyetrace.Entity:GetName( ).."Right click to stun")  
 self:tasePlayer(eyetrace.Entity)    // If the it is a player then bring them down tranqPlayer()
 else return 
 end
 if eyetrace.Entity:IsNPC() and
 math.Dist(self:GetPos(), eyetrace:GetPos()) > 50 then
 self.Owner:PrintMessage( HUD_PRINTCENTER, "Now right click to electrocute the NPC. Right click to stun" )
 self:taseNPC(eyetrace.Entity, self.Owner)    // If the it is a NPC then bring them down with tranqNPC()
 else return
 end
end

As you can see I tried to use math.Dist to accomplice this( Didnt work too well…)

After that I tried to replace the math.Dist lines with


self:GetPos():eyetrace:GetPos() > 50 then

That didnt work either.

the error: [ERROR] addons/realistic taser/lua/weapons/taser/shared.lua:510: attempt to call method ‘GetPos’ (a nil value) 1. unknown - addons/realistic taser/lua/weapons/taser/shared.lua:510

Any ideas?

[lua]
eyetrace it’s not a method, so don’t call it as metod
self:GetPos():eyetrace:GetPos() > 50 then

local eyetrace = self.Owner:GetEyeTrace();
Print(eyetrace.HitPos) //return where the eyetrace hits

//If you want to get the distance between the eyes and the hit pos

local dist = self.Owner:EyePos():Distance(eyetrace.HitPos)
if(dist < x) then
<do stuff>()
end
[/lua]