SWEP Problem, Please Help!

I’m trying to create a SWEP for GMod 10 that will remove the gravity of a prop when you shoot it. I have coded what I thought would do this but I keep getting the ‘attempt to index’ error for ‘Owner’ and sometimes even ‘self’.

Any ideas on how to fix this or correctly make this weapon?
if (SERVER) then
AddCSLuaFile (“shared.lua”);

SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;


if (CLIENT) then

SWEP.PrintName = "AntiGravity Gun";
SWEP.Slot = 3;
SWEP.SlotPos = 1;
SWEP.DrawAmmo = false;
SWEP.DrawCrosshair = true;


SWEP.Author = “Andrew Brown”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “Left Click Removes Gravity, Right Click Reapplies Gravity”

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_pistol.mdl”
SWEP.WorldModel = “models/weapons/w_pistol.mdl”

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “none”

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

local ShootSound = Sound (“Metal.SawbladeStick”);

function SWEP:Reload()

function SWEP:Think()

function SWEP:Initialize()
self:SetWeaponHoldType (self.HoldType)

function SWEP:antigravity()

//local PlayerPos = self.Owner:GetShootPos(); 
//local tr = self.Owner:GetEyeTrace();

//self.Weapon:EmitSound (ShootSound);
//self.BaseClass.ShootEffects (self);

//if (!SERVER) then return end

local ply = self:GetOwner()

local ent = ply:GetEyeTrace().Entity
if (ent:IsValid()) then end

local PhysObj = ent:GetPhysicsObject();
if PhysObj:IsValid()then

cleanup.Add (self.Owner, "props", ent);

undo.Create ("Prop");
undo.AddEntity (ent);
undo.SetPlayer (ply);


function SWEP:PrimaryAttack()
self.antigravity ();

function SWEP:SecondaryAttack()
//self.gravity ();

First off use the code tags.
Now onto the code. I noticed that in your gravity function you have

if (ent:IsValid()) then end

That end should be before your last end.  You are also missing another end for the last if statement in that function.  As to why the self isn't working I do not know.  I tried the code myself and after fixing those 2 ends I got the same error you did with self being nil.  BUT once I put all the code into the primaryAttack function it worked.  So maybe instead of calling the function just throw all the code in there.

So the code below does work but only when it’s in the primaryAttack function

function SWEP:PrimaryAttack()	
   if (!SERVER) then return end
   local ent = self.Owner:GetEyeTrace().Entity
   if (ent:IsValid()) then 
      local PhysObj = ent:GetPhysicsObject();
      if PhysObj:IsValid()then

Thanks so much! I would never have caught that on my own. I’m new to this and I really appreciate the help. =)