Garry's Mod Wiki

GM:PlayerCanPickupWeapon

  boolean GM:PlayerCanPickupWeapon( Player ply, Weapon weapon )

Description

Returns whether or not a player is allowed to pick up a weapon.

Arguments

1 Player ply
The player attempting to pick up the weapon.
2 Weapon weapon
The weapon entity in question.

Returns

1 boolean
Allowed pick up or not.

Example

Disallows picking up a weapon if player already has this weapon ( prevents ammo pickups from lying guns ).

hook.Add( "PlayerCanPickupWeapon", "noDoublePickup", function( ply, weapon ) if ( ply:HasWeapon( weapon:GetClass() ) ) then return false end end )

Example

Players can only pick up the HL2 Pistol.

hook.Add( "PlayerCanPickupWeapon", "OnlyPistol", function( ply, weapon ) return ( weapon:GetClass() == "weapon_pistol" ) end )

Example

How you could give a player an alternate weapon to the one they picked up (such as an RPG Launcher rather than a pistol).

hook.Add( "PlayerCanPickupWeapon", "NoPistolGiveFists", function( ply, weapon ) if ( weapon:GetClass() == "weapon_pistol" ) then -- if the weapon they are trying to pick up is a pistol ply:Give( "weapon_rpg" ) -- give them an RPG weapon:Remove() -- remove the one they were trying to pick up return false -- don't give them a pistol end end )