Garry's Mod Wiki

WEAPON:PrimaryAttack

  WEAPON:PrimaryAttack()
This hook is predicted. This means that in singleplayer, it will not be called in the Client realm.

Description

Called when primary attack button ( +attack ) is pressed.

When in singleplayer, this function is only called in the server realm. When in multiplayer, the hook will be called on both the server and the client in order to allow for Prediction.

You can force the hook to always be called on client like this:

if ( game.SinglePlayer() ) then self:CallOnClient( "PrimaryAttack" ) end

Note that due to prediction, in multiplayer SWEP:PrimaryAttack is called multiple times per one "shot" with the gun. To work around that, use IsFirstTimePredicted.

Example

This is how it is defined in weapon_base

function SWEP:PrimaryAttack() -- Make sure we can shoot first if ( !self:CanPrimaryAttack() ) then return end -- Play shoot sound self.Weapon:EmitSound("Weapon_AR2.Single") -- Shoot 1 bullet, 150 damage, 0.01 aimcone self:ShootBullet( 150, 1, 0.01 ) -- Remove 1 bullet from our clip self:TakePrimaryAmmo( 1 ) -- Punch the player's view self:GetOwner():ViewPunch( Angle( -1, 0, 0 ) ) end