HALP With Poison Dart Gun

Me and my friend coded this poison dart gun that works fine when shooting players but doesnt subtract ammo or fire when shot somewhere that is NOT a player. Also there is no firing animation for the shooter.



if SERVER then
	AddCSLuaFile( "shared.lua" )
end

SWEP.HoldType			= "pistol"

if CLIENT then
	SWEP.PrintName = "Poison Dart Gun"
	SWEP.Slot = 6
	
	SWEP.EquipMenuData = {
	type = "item_weapon",
	desc = "Poison your friends! Poison your enemies! You can even poison you family!"
	};
	
	SWEP.Icon = "VGUI/ttt/icon_silenced"
end

SWEP.Base = "weapon_tttbase"
SWEP.Primary.Recoil	= 1.35
SWEP.Primary.Damage = 0
SWEP.Primary.Delay = 0.38
SWEP.Primary.Cone = 0.02
SWEP.Primary.ClipSize = 1
SWEP.Primary.Automatic = true
SWEP.Primary.DefaultClip = 2
SWEP.Primary.ClipMax = 2
SWEP.Primary.Ammo = "Pistol"

SWEP.Kind = WEAPON_EQUIP
SWEP.CanBuy = {ROLE_TRAITOR} -- only traitors can buy
SWEP.WeaponID = AMMO_SIPISTOL

SWEP.AmmoEnt = "item_ammo_pistol_ttt"

SWEP.IsSilent = true

SWEP.UseHands			= true
SWEP.ViewModelFlip		= false
SWEP.ViewModelFOV		= 54
SWEP.ViewModel			= "models/weapons/cstrike/c_pist_usp.mdl"
SWEP.WorldModel			= "models/weapons/w_pist_usp.mdl"

SWEP.Primary.Sound = Sound( "weapons/usp/usp1.wav" )
SWEP.Primary.SoundLevel = 50

SWEP.IronSightsPos = Vector( -5.91, -4, 2.84 )
SWEP.IronSightsAng = Vector(-0.5, 0, 0)

SWEP.PrimaryAnim = ACT_VM_PRIMARYATTACK_SILENCED
SWEP.ReloadAnim = ACT_VM_RELOAD_SILENCED

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW_SILENCED)
	return true
end

local curMaxTimerId = 0

function GetNewTimerName()
	curMaxTimerId = curMaxTimerId + 1
	return "PoisonDartTimer" .. curMaxTimerId
end



function SWEP:PrimaryAttack()
	if SERVER then
		if self:Clip1() >= 1 then
			if not IsValid(ply) then return end
			local ply = self.Owner     
			local damagePerHit = 5
			local spos = ply:GetShootPos()
			local epos = spos + ply:GetAimVector() * 100000
			local ignore = {ply, self.Weapon}
			local victim = util.TraceLine({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID}).Entity
			if victim and victim.IsPlayer then
				local timerName
				local function damagePlayer()
				local curHealth = victim:Health()
				if IsValid(victim) and victim:Alive() then
					if curHealth <= 0 then
						victim:Kill()
					end
					local newHealth = curHealth - damagePerHit
					victim:SetHealth(newHealth)
				elseif timerName then
					timer.Destroy(timerName)
				end
			end
			timerName = GetNewTimerName()
			timer.Create(timerName, 0.7, 0, damagePlayer)
			victim:PrintMessage(HUD_PRINTCENTER, "You have been poisoned!")
		elseif self:Clip1() <= 0 then
			self:Reload()
		end
		local ply = self.Owner
		ply:SetAnimation(PLAYER_ATTACK1)
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self.Owner:EmitSound( "weapons/usp/usp1.wav" ) 
		self:TakePrimaryAmmo( 1 )
	end
end
	
if CLIENT then
	local ply = self.Owner
	if not IsValid(ply) then return end
		ply:SetAnimation(ACT_VM_PRIMARYATTACK_SILENCED)
	end
end

function SWEP:Reload()
	self:SetIronsights( false )
	
	if self.ReloadingTime and CurTime() <= self.ReloadingTime then return end
	
	if ( self:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 ) then
		
		self:DefaultReload( ACT_VM_RELOAD_SILENCED )
		local AnimationTime = self.Owner:GetViewModel():SequenceDuration()
		self.ReloadingTime = CurTime() + AnimationTime
		self:SetNextPrimaryFire(CurTime() + AnimationTime)
		self:SetNextSecondaryFire(CurTime() + AnimationTime)
		
	end
	
end

-- We were bought as special equipment, and we have an extra to give
function SWEP:WasBought(buyer)
	if IsValid(buyer) then -- probably already self.Owner
		buyer:GiveAmmo( 2, "Poison Dart Gun" )
	end
end


[editline]23rd December 2013[/editline]

I fixed everything but the lack of animations and sound for the clientside shooter and that when the clip has zero bullets but there is still one or more bullets left, it will not auto reload on primary fire, everything else is fixed, but cant figure this last thing out! Please help.

New Code:



if SERVER then
	AddCSLuaFile( "shared.lua" )
end

SWEP.HoldType			= "pistol"

if CLIENT then
	SWEP.PrintName = "Poison Dart Gun"
	SWEP.Slot = 6
	
	SWEP.EquipMenuData = {
	type = "item_weapon",
	desc = "Poison your friends! Poison your enemies! You can even poison you family!"
	};
	
	SWEP.Icon = "VGUI/ttt/icon_silenced"
end

SWEP.Base = "weapon_tttbase"
SWEP.Primary.Recoil	= 1.35
SWEP.Primary.Damage = 0
SWEP.Primary.Delay = 0.38
SWEP.Primary.Cone = 0.02
SWEP.Primary.ClipSize = 1
SWEP.Primary.Automatic = true
SWEP.Primary.DefaultClip = 2
SWEP.Primary.ClipMax = 2
SWEP.Primary.Ammo = "Pistol"

SWEP.Kind = WEAPON_EQUIP
SWEP.CanBuy = {ROLE_TRAITOR} -- only traitors can buy
SWEP.WeaponID = AMMO_SIPISTOL

SWEP.AmmoEnt = "item_ammo_pistol_ttt"

SWEP.IsSilent = true

SWEP.UseHands			= true
SWEP.ViewModelFlip		= false
SWEP.ViewModelFOV		= 54
SWEP.ViewModel			= "models/weapons/cstrike/c_pist_usp.mdl"
SWEP.WorldModel			= "models/weapons/w_pist_usp.mdl"

SWEP.Primary.Sound = Sound( "weapons/usp/usp1.wav" )
SWEP.Primary.SoundLevel = 50

SWEP.IronSightsPos = Vector( -5.91, -4, 2.84 )
SWEP.IronSightsAng = Vector(-0.5, 0, 0)

SWEP.PrimaryAnim = ACT_VM_PRIMARYATTACK_SILENCED
SWEP.ReloadAnim = ACT_VM_RELOAD_SILENCED

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW_SILENCED)
	return true
end

local curMaxTimerId = 0

function GetNewTimerName()
	curMaxTimerId = curMaxTimerId + 1
	return "PoisonDartTimer" .. curMaxTimerId
end



function SWEP:PrimaryAttack()
	if SERVER then
		--self.Owner:PrintMessage(HUD_PRINTCENTER, "this was also called")
		if self:Clip1() >= 1 then
			--self.Owner:PrintMessage(HUD_PRINTCENTER, "You have a bullet!")
			local ply = self.Owner     
			local damagePerHit = 5
			local spos = ply:GetShootPos()
			local epos = spos + ply:GetAimVector() * 100000
			local ignore = {ply, self.Weapon}
			local victim = util.TraceLine({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID}).Entity
			if IsValid(victim) and victim.IsPlayer then
				--self.Owner:PrintMessage(HUD_PRINTCENTER, "You hit a player!")
				self.Owner:PrintMessage(HUD_PRINTCENTER, "You have poisoned a player!")
				local timerName
				local function damagePlayer()
				local curHealth = victim:Health()
				if IsValid(victim) and victim:Alive() then
					if curHealth <= 0 then
						victim:Kill()
					end
					local newHealth = curHealth - damagePerHit
					victim:SetHealth(newHealth)
				elseif timerName then
					timer.Destroy(timerName)
				end
			end
			timerName = GetNewTimerName()
			timer.Create(timerName, 0.7, 0, damagePlayer)
			victim:PrintMessage(HUD_PRINTCENTER, "You have been poisoned!")
		elseif not self:Clip1() == 1 and self.Primary.Ammo >= 1 then
			self:Reload()
			--self.Owner:PrintMessage(HUD_PRINTCENTER, "You need to reload!")
		end
		local ply = self.Owner
		--self.Owner:PrintMessage(HUD_PRINTCENTER, "You shot a bullet!")
		ply:SetAnimation(PLAYER_ATTACK1)
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self.Owner:EmitSound( "weapons/usp/usp1.wav" ) 
		self:TakePrimaryAmmo( 1 )
	end
end
	
if CLIENT then
	local ply = self.Owner
	if not IsValid(ply) then return end
		ply:SetAnimation(ACT_VM_PRIMARYATTACK_SILENCED)
	end
end

function SWEP:Reload()
	self:SetIronsights( false )
	
	if self.ReloadingTime and CurTime() <= self.ReloadingTime then return end
	
	if ( self:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 ) then
		
		self:DefaultReload( ACT_VM_RELOAD_SILENCED )
		local AnimationTime = self.Owner:GetViewModel():SequenceDuration()
		self.ReloadingTime = CurTime() + AnimationTime
		self:SetNextPrimaryFire(CurTime() + AnimationTime)
		self:SetNextSecondaryFire(CurTime() + AnimationTime)
		
	end
	
end

-- We were bought as special equipment, and we have an extra to give
function SWEP:WasBought(buyer)
	if IsValid(buyer) then -- probably already self.Owner
		buyer:GiveAmmo( 2, "Poison Dart Gun" )
	end
end



Worked on it a little more, now were just missing the clientside shooting animation.

New new code:



if SERVER then
	AddCSLuaFile( "shared.lua" )
end

SWEP.HoldType			= "pistol"

if CLIENT then
	SWEP.PrintName = "Poison Dart Gun"
	SWEP.Slot = 6
	
	SWEP.EquipMenuData = {
	type = "item_weapon",
	desc = "Poison your friends! Poison your enemies! You can even poison you family!"
	};
	
	SWEP.Icon = "VGUI/ttt/icon_silenced"
end

SWEP.Base = "weapon_tttbase"
SWEP.Primary.Recoil	= 1.35
SWEP.Primary.Damage = 0
SWEP.Primary.Delay = 0.38
SWEP.Primary.Cone = 0.02
SWEP.Primary.ClipSize = 1
SWEP.Primary.Automatic = true
SWEP.Primary.DefaultClip = 2
SWEP.Primary.ClipMax = 2
SWEP.Primary.Ammo = "Pistol"

SWEP.Kind = WEAPON_EQUIP
SWEP.CanBuy = {ROLE_TRAITOR} -- only traitors can buy
SWEP.WeaponID = AMMO_SIPISTOL

SWEP.AmmoEnt = "none"

SWEP.IsSilent = true

SWEP.UseHands			= true
SWEP.ViewModelFlip		= false
SWEP.ViewModelFOV		= 54
SWEP.ViewModel			= "models/weapons/cstrike/c_pist_usp.mdl"
SWEP.WorldModel			= "models/weapons/w_pist_usp.mdl"

SWEP.Primary.Sound = Sound( "weapons/usp/usp1.wav" )
SWEP.Primary.SoundLevel = 50

SWEP.IronSightsPos = Vector( -5.91, -4, 2.84 )
SWEP.IronSightsAng = Vector(-0.5, 0, 0)

SWEP.PrimaryAnim = ACT_VM_PRIMARYATTACK_SILENCED
SWEP.ReloadAnim = ACT_VM_RELOAD_SILENCED

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW_SILENCED)
	return true
end

local curMaxTimerId = 0

function GetNewTimerName()
	curMaxTimerId = curMaxTimerId + 1
	return "PoisonDartTimer" .. curMaxTimerId
end



function SWEP:PrimaryAttack()
	if SERVER then
		--self.Owner:PrintMessage(HUD_PRINTCENTER, "this was also called")
		if self:Clip1() >= 1 then
			--self.Owner:PrintMessage(HUD_PRINTCENTER, "You have a bullet!")
			local ply = self.Owner     
			local damagePerHit = 5
			local spos = ply:GetShootPos()
			local epos = spos + ply:GetAimVector() * 100000
			local ignore = {ply, self.Weapon}
			local victim = util.TraceLine({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID}).Entity
			if IsValid(victim) and victim.IsPlayer then
				--self.Owner:PrintMessage(HUD_PRINTCENTER, "You hit a player!")
				self.Owner:PrintMessage(HUD_PRINTCENTER, "You have poisoned a player!")
				local timerName
				local function damagePlayer()
					local curHealth = victim:Health()
					if IsValid(victim) and victim:Alive() then
						if curHealth <= 0 then
							victim:Kill()
						end
						local newHealth = curHealth - damagePerHit
						victim:SetHealth(newHealth)
					elseif timerName then
						timer.Destroy(timerName)
					end
				end
				timerName = GetNewTimerName()
				timer.Create(timerName, 0.7, 0, damagePlayer)
				victim:PrintMessage(HUD_PRINTCENTER, "You have been poisoned!")
			end
			--elseif not self:Clip1() == 1 and self.Primary.Ammo >= 1 then
		--elseif self:Clip1() <= 0 then
				local ply = self.Owner
		--self.Owner:PrintMessage(HUD_PRINTCENTER, "You shot a bullet!")
		ply:SetAnimation(PLAYER_ATTACK1)
		ply:SetAnimation(ACT_VM_PRIMARYATTACK_SILENCED)
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self.Owner:EmitSound( "weapons/usp/usp1.wav" ) 
		self:TakePrimaryAmmo( 1 )
		else
			self:Reload()
			self.Owner:PrintMessage(HUD_PRINTCENTER, "You need to reload!")
		end
	end
	
	if CLIENT then
		local ply = self.Owner
		if not IsValid(ply) then return end
		ply:SetAnimation(ACT_VM_PRIMARYATTACK_SILENCED)
	end
end

function SWEP:Reload()
	self:SetIronsights( false )
	
	if self.ReloadingTime and CurTime() <= self.ReloadingTime then return end
	
	if ( self:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 ) then
		
		self:DefaultReload( ACT_VM_RELOAD_SILENCED )
		local AnimationTime = self.Owner:GetViewModel():SequenceDuration()
		self.ReloadingTime = CurTime() + AnimationTime
		self:SetNextPrimaryFire(CurTime() + AnimationTime)
		self:SetNextSecondaryFire(CurTime() + AnimationTime)
		
	end
	
end

-- We were bought as special equipment, and we have an extra to give
function SWEP:WasBought(buyer)
	if IsValid(buyer) then -- probably already self.Owner
		buyer:GiveAmmo( 2, "Poison Dart Gun" )
	end
end



I think you seem to be helping yourself rather fine, silly.