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?
Code:
if (SERVER) then
AddCSLuaFile ("shared.lua");
SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;
end
if (CLIENT) then
SWEP.PrintName = "AntiGravity Gun";
SWEP.Slot = 3;
SWEP.SlotPos = 1;
SWEP.DrawAmmo = false;
SWEP.DrawCrosshair = true;
end
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()
end
function SWEP:Think()
end
function SWEP:Initialize()
self:SetWeaponHoldType (self.HoldType)
end
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
PhysObj:EnableGravity(false)
end
cleanup.Add (self.Owner, "props", ent);
undo.Create ("Prop");
undo.AddEntity (ent);
undo.SetPlayer (ply);
undo.Finish();
end
function SWEP:PrimaryAttack()
self.antigravity ();
end
function SWEP:SecondaryAttack()
//self.gravity ();
end
First off use the code tags.
Now onto the code. I noticed that in your gravity function you have
[code]if (ent:IsValid()) then end[/code] 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
[code]
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
PhysObj:EnableGravity(false)
end
end
end
[/code]
[QUOTE=S W;23748289]First off use the code tags.
Now onto the code. I noticed that in your gravity function you have
[code]if (ent:IsValid()) then end[/code] 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
[code]
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
PhysObj:EnableGravity(false)
end
end
end
[/code][/QUOTE]
Thanks so much! I would never have caught that on my own. I'm new to this and I really appreciate the help. =)
Sorry, you need to Log In to post a reply to this thread.