SWEP doesn't fire

Hi people, I’m having a problem with my first SWEP. For some reason, it doesn’t fire. This is the PrimaryAttack function:


function SWEP:PrimaryAttack()
		if ( self.Owner:GetAmmoCount( self:GetPrimaryAmmoType() ) > 0 ) then
			local ent = ents.Create( "ent_soundgrenade" )
			local Forward = self.Owner:EyeAngles():Forward()
			local Right = self.Owner:EyeAngles():Right()
			local Up = self.Owner:EyeAngles():Up()
			ent:SetNetworkedBool( "Active", false )
			ent:SetPos( self.Owner:GetShootPos() + Forward * 8 + Right * 6 + Up * -3)
			ent:SetOwner(self.Owner)
			ent:Spawn()
			ent:Activate()
			ent:GetPhysicsObject():ApplyForceCenter( self.Owner:GetAimVector() * 4000 )
			
			self.Weapon:SendWeaponAnim( ACT_VM_THROW )
			self.Weapon:SetNextPrimaryFire( CurTime() + 1.2 )
			self.Weapon:SetNextSecondaryFire( CurTime() + 1.2 )

			undo.Create("sound_grenade")
			undo.AddEntity( ent )
			undo.SetPlayer( self.Owner )
			undo.Finish()

			timer.Create( "anim_timer"..tostring(self.Weapon), 0.5, 1, function() self.Weapon:SendWeaponAnim( ACT_VM_DRAW ) end )
			self:TakePrimaryAmmo( 1 )
		end
	end

If I do <ammo == 0>, the weapon works fine, so I guess there’s something wrong with the custom ammo type, as I cannot see my ammo count either. This is the first part of the file:


if SERVER then
AddCSLuaFile("wpn_soundgrenade.lua")
SWEP.Weight			= 5
SWEP.AutoSwitchTo		= true
SWEP.AutoSwitchFrom		= true
end

if CLIENT then
language.Add("wpn_soundgrenade", "Sound Grenade")

SWEP.Category 		= "Other"
SWEP.PrintName = "Sound Grenade"
SWEP.Slot = 4
SWEP.SlotPos = 4
SWEP.DrawAmmo = true
SWEP.DrawCrosshair = true
SWEP.ViewModelFOV = 55
SWEP.ViewModelFlip = false
SWEP.DrawWeaponInfoBox	= false
SWEP.BounceWeaponIcon = false 
end

SWEP.HoldType		= "grenade"
SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true
SWEP.UseHands			= true
SWEP.ViewModel			= "models/weapons/c_grensnd.mdl"
SWEP.WorldModel			= "models/weapons/w_grensnd.mdl"

game.AddAmmoType( { name = "sm_soundgrenade" , maxcarry = 3} )
if ( CLIENT ) then language.Add( "soundgrenade_ammo", "Sound Grenade" ) end

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= 1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "sm_soundgrenade"
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo		= "none"

I copied it from an example weapon, which works perfectly fine, so I don’t understand why my weapon is not working. Any ideas?

Bump :cry:

When I do “lua_run print(game.GetAmmoID(“sm_soundgrenade”))” in console it prints “-1”, so the ammo type is not being created for some reason. Anyone knows why?

Try filling out the entire table on game.AddAmmoType. Usually, arguments for function have default values, and not filling them in won’t be an issue. However, by filling in the first argument for game.AddAmmoType, it probably means that none of the other values inside of the table are passed, which might just make GMod not even try to add the ammo type.

If that doesn’t work, try taking the line



game.AddAmmoType( { name = "sm_soundgrenade" , maxcarry = 3} )


and allowing it to run on client and server, in case that makes a difference for some odd reason, although the wiki says it should run clientside and serverside, so I highly doubt this is it.

Nope, nothing.

However, I used a while loop to print all ammo types, and appaently I have exactly 128. The fact that it is a power of 2 (2^7) makes me think that the game has a hardcoded limit of 128 custom ammo types. If that’s the case, well that sucks. I’ll try to unsuscribe to some mods and see if it that solves my problem.

[editline]26th February 2016[/editline]

Just as I thought, there’s a hardcoded limit of 128 ammo types. Thats why some weapons stop working when you have too many weapon addons. I removed some, and now my weapon works fine.

I also noticed that the function “game.AddAmmoType” doesn’t check if that type already exists, so the list quickly fills up with duplicates that probably will never be used. I modified that lua file so it doesn’t creates duplicates anymore. Now I can use all my weapon addons without reaching the limit.