Garry's Mod Wiki

GM:PlayerSwitchWeapon

  boolean GM:PlayerSwitchWeapon( Player player, Weapon oldWeapon, Weapon newWeapon )
This hook is predicted. This means that in singleplayer, it will not be called in the Client realm.

Description

Called when a player attempts to switch their weapon.

Primary usage of this hook is to prevent/allow weapon switching, not to detect weapon switching. It will not be called for Player:SetActiveWeapon.

Arguments

1 Player player
The player switching weapons.
2 Weapon oldWeapon
The previous weapon. Will be NULL if the previous weapon was removed or the player is switching from nothing.
3 Weapon newWeapon
The weapon the player switched to. Will be NULL if the player is switching to nothing.
This can be NULL on the client if the weapon hasn't been created over the network yet.

Issue Tracker: 2922

Returns

1 boolean
Return true to prevent weapon switch.

Example

The players weapon information will be printed when the player switched weapons.

hook.Add( "PlayerSwitchWeapon", "WeaponSwitchExample", function( ply, oldWeapon, newWeapon ) -- GetClass() will return the weapons class as a string. MsgN( "You switched weapons! Your old weapon is " .. oldWeapon:GetClass() .."." ) MsgN( "Your new weapon is " .. newWeapon:GetClass() .. "." ) end )
Output:
You switched weapons! Your old weapon is gmod_camera. Your new weapon is weapon_crossbow.