Need help with a SWEP

I’ve been having issues with this SWEP. It was working for a while but as I changed stuff it proceeded to break. So far, it refuses to show up in the spawn menu. It also does not work with the F4 buy menu in DarkRP. It shows up but if you buy it, the weapon doesn’t spawn in the world. This seems to be the chunk of code causing most of the problems…


if(CLIENT)then
        function SWEP:GetViewModelPosition(pos,ang)
                if not(self.DownAmt)then self.DownAmt=8 end
                if(self.Owner:KeyDown(IN_SPEED))then
                        self.DownAmt=math.Clamp(self.DownAmt+.1,0,8)
                else
                        self.DownAmt=math.Clamp(self.DownAmt-.1,0,8)
                end
                local NewPos=pos+ang:Forward()*30-ang:Up()*(12+self.DownAmt)+ang:Right()*10
                return NewPos,ang
        end
        function SWEP:DrawWorldModel()
local Pos,Ang=self.Owner:GetBonePosition(self.Owner:LookupBone("ValveBiped.Bip01_R_Hand"))
                if(self.DatWorldModel && Pos && Ang)then
                                        self:DrawModel()
                                end
                self.DatWorldModel:SetRenderOrigin(Pos+Ang:Forward()*6-Ang:Up()*2+Ang:Right()*1)
                Ang:RotateAroundAxis(Ang:Right(),-30)
                self.DatWorldModel:SetRenderAngles(Ang)
                self.DatWorldModel:DrawModel()
end
                        self.DatWorldModel=ClientsideModel("models/weapons/w_models/w_jyringe_proj.mdl")
                        self.DatWorldModel:SetPos(self:GetPos())
                        self.DatWorldModel:SetParent(self)
                        self.DatWorldModel:SetNoDraw(true)
                        self.DatWorldModel:SetModelScale(.5,0)
                end
        function SWEP:ViewModelDrawn()
                --
        end

I had an error in the console just now:


[Batrachotoxin Needle] lua/weapons/wep_jack_hmcd_poisonneedle.lua:154: attempt to index global 'self' (a nil value)
  1. unknown - lua/weapons/wep_jack_hmcd_poisonneedle.lua:154

Any help is appreciated.

Just in case, here’s the full file:


if(SERVER)then
	AddCSLuaFile()
elseif(CLIENT)then
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false

	SWEP.ViewModelFOV = 65

	SWEP.Slot = 3
	SWEP.SlotPos = 1

	killicon.AddFont("wep_jack_hmcd_poisonneedle", "HL2MPTypeDeath", "5", Color(0, 0, 255, 255))

	function SWEP:DrawViewModel()
		return false
	end

	function SWEP:DrawWorldModel()	
		self:DrawModel()
	end

	function SWEP:DrawHUD()
		--
	end
end

SWEP.Base="weapon_base"

SWEP.ViewModel = "models/weapons/w_models/w_jyringe_proj.mdl"
SWEP.WorldModel = "models/weapons/w_models/w_jyringe_proj.mdl"
if(CLIENT)then SWEP.WepSelectIcon=surface.GetTextureID("vgui/wep_jack_hmcd_poisonneedle");SWEP.BounceWeaponIcon=false end
SWEP.PrintName = "Batrachotoxin Needle"
SWEP.Instructions	= "This is a tiny improvised syringe with an extremely small, sharp needle and a few milligrams of deadly neurotoxin. Use it to kill very discreetly.

LMB to prick someone.
Single-use.
Takes time to work.
Take care that the victim does not notice."
SWEP.Author			= "Originally made by Jackarunda, made into a SWEP by Wolf"
SWEP.Contact		= "octanewolf@site.nfoservers.com"
SWEP.Purpose		= "Silently kill your target"
SWEP.BobScale=2
SWEP.SwayScale=2
SWEP.Weight	= 3
SWEP.AutoSwitchTo		= true
SWEP.AutoSwitchFrom		= false

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.Category = "Batrachotoxin Needle"


SWEP.Primary.Delay			= 0.5
SWEP.Primary.Recoil			= 3
SWEP.Primary.Damage			= 120
SWEP.Primary.NumShots		= 1	
SWEP.Primary.Cone			= 0.04
SWEP.Primary.ClipSize		= -1
SWEP.Primary.Force			= 900
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic   	= true
SWEP.Primary.Ammo         	= "none"

SWEP.Secondary.Delay		= 0.9
SWEP.Secondary.Recoil		= 0
SWEP.Secondary.Damage		= 0
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Cone			= 0
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic   	= false
SWEP.Secondary.Ammo         = "none"

function SWEP:Initialize()
	self:SetHoldType("normal")
end

function SWEP:SetupDataTables()
	--
end

function SWEP:PrimaryAttack()
	if not(IsFirstTimePredicted())then return end
	if(self.Owner:KeyDown(IN_SPEED))then return end
	self:SetNextPrimaryFire(CurTime()+1)
	self:AttackFront()
end

function SWEP:Deploy()
	if not(IsFirstTimePredicted())then return end
	self.DownAmt=8
	self:SetNextPrimaryFire(CurTime()+1)
	return true
end

function SWEP:Holster()
	return true
end

function SWEP:OnRemove()
	--
end

function SWEP:SecondaryAttack()
	--
end

function SWEP:Think()
	--
end



function SWEP:AttackFront()
	if(CLIENT)then return end
	self.Owner:LagCompensation(true)
	local Ent,HitPos,HitNorm=HMCD_WhomILookinAt(self.Owner,.2,50)
	local AimVec,Mul=self.Owner:GetAimVector(),1
	if((IsValid(Ent))and(Ent:IsPlayer()))then
		sound.Play("snd_jack_hmcd_needleprick.wav",self.Owner:GetShootPos(),45,math.random(90,110))
		sound.Play("snd_jack_hmcd_needleprick.wav",HitPos,40,math.random(90,110))
		self.Owner:ViewPunch(Angle(1,0,0))
		Ent:ViewPunch(Angle(-.05,0,0))
		-- covert poisoning FTW
		HMCD_Poison(Ent,self.Owner)
		self:Remove()
	else
		sound.Play("snd_jack_hmcd_tinyswish.wav",self.Owner:GetShootPos(),45,math.random(90,110))
	end
	self.Owner:LagCompensation(false)
end

function SWEP:Reload()
	--
end

if(CLIENT)then
        function SWEP:GetViewModelPosition(pos,ang)
                if not(self.DownAmt)then self.DownAmt=8 end
                if(self.Owner:KeyDown(IN_SPEED))then
                        self.DownAmt=math.Clamp(self.DownAmt+.1,0,8)
                else
                        self.DownAmt=math.Clamp(self.DownAmt-.1,0,8)
                end
                local NewPos=pos+ang:Forward()*30-ang:Up()*(12+self.DownAmt)+ang:Right()*10
                return NewPos,ang
        end
        function SWEP:DrawWorldModel()
local Pos,Ang=self.Owner:GetBonePosition(self.Owner:LookupBone("ValveBiped.Bip01_R_Hand"))
                if(self.DatWorldModel && Pos && Ang)then
                                        self:DrawModel()
                                end
                self.DatWorldModel:SetRenderOrigin(Pos+Ang:Forward()*6-Ang:Up()*2+Ang:Right()*1)
                Ang:RotateAroundAxis(Ang:Right(),-30)
                self.DatWorldModel:SetRenderAngles(Ang)
                self.DatWorldModel:DrawModel()
end
                        self.DatWorldModel=ClientsideModel("models/weapons/w_models/w_jyringe_proj.mdl")
                        self.DatWorldModel:SetPos(self:GetPos())
                        self.DatWorldModel:SetParent(self)
                        self.DatWorldModel:SetNoDraw(true)
                        self.DatWorldModel:SetModelScale(.5,0)
                end
        function SWEP:ViewModelDrawn()
                --
        end


function HMCD_WhomILookinAt(ply,cone,dist)
	local CreatureTr,ObjTr,OtherTr=nil,nil,nil
	for i=1,(150*cone) do
		local Vec=(ply:GetAimVector()+VectorRand()*cone):GetNormalized()
		local Tr=util.QuickTrace(ply:GetShootPos(),Vec*dist,{ply})
		if((Tr.Hit)and not(Tr.HitSky)and(Tr.Entity))then
			local Ent,Class=Tr.Entity,Tr.Entity:GetClass()
			if((Ent:IsPlayer())or(Ent:IsNPC()))then
				CreatureTr=Tr
			elseif((Class=="prop_physics")or(Class=="prop_physics_multiplayer")or(Class=="prop_ragdoll")or(Ent.IsLoot))then
				ObjTr=Tr
			else
				OtherTr=Tr
			end
		end
	end
	if(CreatureTr)then return CreatureTr.Entity,CreatureTr.HitPos,CreatureTr.HitNormal end
	if(ObjTr)then return ObjTr.Entity,ObjTr.HitPos,ObjTr.HitNormal end
	if(OtherTr)then return OtherTr.Entity,OtherTr.HitPos,OtherTr.HitNormal end
	return nil,nil,nil
end

HMCD_DamageTypes={
	[DMG_POISON]="poisoned"
}

function HMCD_Poison(vic,murd)
	if not(vic.LifeID)then vic.LifeID=1 end
	local Victim,Murderer,LifeID=vic,murd,vic.LifeID
	timer.Simple(math.random(30,60),function()
		if((Victim)and(IsValid(Victim))and(Victim:Alive())and(Victim.LifeID==LifeID))then
			timer.Simple(math.random(2,7),function()
				if((IsValid(Victim))and(Victim:Alive())and(Victim.LifeID==LifeID))then
					Victim.TempSpeedMul=.1
					timer.Simple(math.random(2,7),function()
						if((IsValid(Victim))and(Victim:Alive())and(Victim.LifeID==LifeID))then
							Victim.TempSpeedMul=.1
							timer.Simple(.1,function()
								local Dmg=DamageInfo()
								Dmg:SetDamage(105)
								Dmg:SetDamageType(DMG_POISON)
								Dmg:SetAttacker(Murderer)
								Dmg:SetInflictor(Victim)
								Dmg:SetDamagePosition(Victim:GetPos())
								Dmg:SetDamageForce(Vector(0,0,0))
								Victim:TakeDamageInfo(Dmg)
							end)
						end
					end)
				end
			end)
		end
	end)
end

self is nil because you are referencing it outside of a SWEP function. It looks like you intended to call it in DrawWorldModel


function SWEP:DrawWorldModel()
	
	local Pos,Ang=self.Owner:GetBonePosition(self.Owner:LookupBone("ValveBiped.Bip01_R_Hand"))
	
	if(self.DatWorldModel && Pos && Ang)then
		
		self:DrawModel()
		
	end
	
	self.DatWorldModel:SetRenderOrigin(Pos+Ang:Forward()*6-Ang:Up()*2+Ang:Right()*1)
	Ang:RotateAroundAxis(Ang:Right(),-30)
	self.DatWorldModel:SetRenderAngles(Ang)
	self.DatWorldModel:DrawModel()
	
end

self.DatWorldModel=ClientsideModel("models/weapons/w_models/w_jyringe_proj.mdl")
self.DatWorldModel:SetPos(self:GetPos())
self.DatWorldModel:SetParent(self)
self.DatWorldModel:SetNoDraw(true)
self.DatWorldModel:SetModelScale(.5,0)

end

I spaced out the indentation so it’s a little bit more readable, but there is an end before the last 5 lines of the SWEP function. Remove that.

It also looks like there is an end missing at HMCD_WhomILookinAt that will cause an error, so you might want to place one at the end of it.

Thanks for the help. I removed the extra end before the last 5 lines of the SWEP function and added an end at HMCD_WhomILookinAt. However, the weapon no longer wants to attack… no errors though.

My bad, put the end before HMCD_WhomILookinAt

This worked, thanks…
Another error I’ve been getting is whenever someone looks at a player holding the weapon, this error spams the console:


[Batrachotoxin Needle] lua/weapons/batrachotoxin_needle.lua:154: attempt to index field 'DatWorldModel' (a nil value)
  1. unknown - lua/weapons/batrachotoxin_needle.lua:154

Add IsValid checks to DrawWorldModel and the client if statement.

Sorry, kinda new to all this…
where would I put the IsValid checks in the code?

Sorry, kinda new to all this…
where would I put the IsValid checks in the code?



function SWEP:DrawWorldModel()
	if not CLIENT then return end
	local Pos,Ang=self.Owner:GetBonePosition(self.Owner:LookupBone("ValveBiped.Bip01_R_Hand"))
	
	if(self.DatWorldModel && Pos && Ang)then
		
		self:DrawModel()
		
	end
	if IsValid(self.DatWorldModel) then
	self.DatWorldModel:SetRenderOrigin(Pos+Ang:Forward()*6-Ang:Up()*2+Ang:Right()*1)
	Ang:RotateAroundAxis(Ang:Right(),-30)
	self.DatWorldModel:SetRenderAngles(Ang)
	self.DatWorldModel:DrawModel()
	end
end
if IsValid(self.DatWorldModel) then
self.DatWorldModel=ClientsideModel("models/weapons/w_models/w_jyringe_proj.mdl")
self.DatWorldModel:SetPos(self:GetPos())
self.DatWorldModel:SetParent(self)
self.DatWorldModel:SetNoDraw(true)
self.DatWorldModel:SetModelScale(.5,0)
 end
end


This ended up giving another console error, which also broke the weapon:


[Batrachotoxin Needle] lua/weapons/batrachotoxin_needle.lua:163: attempt to index global 'self' (a nil value)
  1. unknown - lua/weapons/batrachotoxin_needle.lua:163

Bump… Still haven’t made any progress.