SWEP Problems

Hey guys, I was wondering if someone could help me…

The primary attack for that fires a harpoon, and the secondary fires a watermelon, the thing is though when I go into its shared.lua and try to change the clip size, it always stays unlimited (using -1 on both clipsize and defaultclipsize)

I also tried changing the delay time (so even though they have unlimited ammo, it’d take a few minutes to be able to re-fire it again), but that still doesnt change either.

I’m trying to use this weapon for an rp server, so having unlimited ammo and fire as fast as you can click is very unrealistic.

Here is the shared.lua:
if ( SERVER ) then

AddCSLuaFile( "shared.lua" )

SWEP.HoldType			= "Pistol"

end

if ( CLIENT ) then

SWEP.PrintName			= "Harpoon Cannon"			
SWEP.Author				= "Fijet"

SWEP.Slot				= 5
SWEP.SlotPos			= 7
SWEP.ViewModelFOV		= 62
SWEP.IconLetter			= "x"

end

------------General Swep Info---------------
SWEP.Author = “Fijet”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “Stick Some Combine To The Walls!”
SWEP.Spawnable = true
SWEP.AdminSpawnable = true

------------Models---------------------------
SWEP.ViewModel = “models/weapons/v_RPG.mdl”
SWEP.WorldModel = “models/weapons/w_rocket_launcher.mdl”

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay = 10 --In seconds
SWEP.Primary.Recoil = 0 --Gun Kick
SWEP.Primary.Damage = 100 --Damage per Bullet
SWEP.Primary.NumShots = 1 --Number of shots per one fire
SWEP.Primary.Cone = 0 --Bullet Spread
SWEP.Primary.ClipSize = -1 --Use “-1 if there are no clips”
SWEP.Primary.DefaultClip = 1 --Number of shots in next clip
SWEP.Primary.Automatic = false --Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo = “none” --Ammo Type
-------------End Primary Fire Attributes------------------------------------

-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay = 5
SWEP.Secondary.Recoil = 0
SWEP.Secondary.Damage = 100
SWEP.Secondary.NumShots = 1
SWEP.Secondary.Cone = 0
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”
-------------End Secondary Fire Attributes--------------------------------

– function SWEP:Reload() --To do when reloading
– end

function SWEP:Think() – Called every frame
end

function SWEP:Initialize()
util.PrecacheSound(“physics/flesh/flesh_impact_bullet” … math.random( 3, 5 ) … “.wav”)
util.PrecacheSound(“weapons/iceaxe/iceaxe_swing1.wav”)
end


function SWEP:PrimaryAttack()
self.Weapon:EmitSound(“weapons/iceaxe/iceaxe_swing1.wav”)
self.Weapon:SetNextPrimaryFire(CurTime() + 0.1)
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
if SERVER then
local bar = ents.Create(“prop_physics”)
bar:SetModel(“models/props_junk/harpoon002a.mdl”)
bar:SetAngles(self.Owner:EyeAngles())-- Angle(0,90,0))
bar:SetPos(self.Owner:GetShootPos())
bar:SetOwner(self.Owner)
bar:SetPhysicsAttacker(self.Owner)
bar:Spawn()
local phys = bar:GetPhysicsObject()
phys:ApplyForceCenter(self.Owner:GetAimVector() * 1999999999)
phys:AddAngleVelocity(Vector(0,5000000,0))
bar:Fire(“kill”, “”, 5)
end
end



function SWEP:SecondaryAttack()
self.Weapon:EmitSound(“weapons/iceaxe/iceaxe_swing1.wav”)
self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
if SERVER then
local bar = ents.Create(“prop_physics”)
bar:SetModel(“models/props_junk/watermelon01.mdl”)
bar:SetAngles(self.Owner:EyeAngles())-- Angle(0,90,0))
bar:SetPos(self.Owner:GetShootPos())
bar:SetOwner(self.Owner)
bar:SetPhysicsAttacker(self.Owner)
bar:Spawn()
local phys = bar:GetPhysicsObject()
phys:ApplyForceCenter(self.Owner:GetAimVector() * 1999999999)
phys:AddAngleVelocity(Vector(0,5000000,0))
bar:Fire(“kill”, “”, 5)
end
end

All help is appreciated!

You need to give it an ammo type other than “none”. The built-in ammo types can be found here.

So, instead of
[lua]
SWEP.Primary.Ammo = “none” --Ammo Type
[/lua]

do something like
[lua]
SWEP.Primary.Ammo = “XBowBolt” --Ammo Type
[/lua]

Keep in mind if the player has crossbow bolt ammo already, this will pull from that same pool.

Go down into PrimaryAttack and modify the line that reads

[lua]
self.Weapon:SetNextPrimaryFire(CurTime() + 0.1)
[/lua]

and change it to
[lua]
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
[/lua]

The modifications will be similar for the secondary fire (just modify SecondaryAttack instead of PrimaryAttack, etc.)

Hm, I tried that and now it says that there is ammo, but if you click it still fires, WELL beyond the clipsize. You can still fire as fast as you can click…And now for some reason the primary attack, the harpoon, doesnt fire at all

[editline]9th August 2011[/editline]

and i cant see the view model either

Oh! I also forgot to say that you need to add something to remove ammo in the PrimaryAttack function. So something like
[lua]
self:TakePrimaryAmmo(1)
[/lua]

For the other errors, I’m not sure. I actually used the base code from the download to test, not what you posted. I’ll check what you posted here in a bit.

Edit: Also, if you wrap your code in lua /lua bbcode tags, it looks a lot nicer.

Edit 2: The best way to implement the ammo level is to override the CanPrimaryAttack function like this:
[lua]
function SWEP:CanPrimaryAttack()
if(self.Owner:GetAmmoCount(self.PrimaryAmmo) <= 0) then
self:EmitSound( “Weapon_Pistol.Empty” )
self:SetNextPrimaryFire( CurTime() + 0.2 )
return false
end

return true

end
[/lua]
(Modify for your own sound, delay, ammo check, etc.)

Then at the beginning of your PrimaryAttack function add
[lua]
if(!self:CanPrimaryAttack()) then return end
[/lua]

Or, you could just add the check for ammo in the PrimaryAttack function too.