Garry's Mod Wiki

Revision Difference

GM:PlayerCanPickupWeapon#528743

<function name="PlayerCanPickupWeapon" parent="GM" type="hook"> <ishook>yes</ishook> <description>Returns whether or not a player is allowed to pick up a weapon.</description> <realm>Server</realm> <predicted>No</predicted> <args> <arg name="ply" type="Player">The player attempting to pick up the weapon</arg>⤶ <arg name="wep" type="Weapon">The weapon entity in question</arg>⤶ <arg name="ply" type="Player">The player attempting to pick up the weapon.</arg>⤶ <arg name="weapon" type="Weapon">The weapon entity in question.</arg>⤶ </args> <rets> <ret name="" type="boolean">Allowed pick up or not</ret>⤶ <ret name="" type="boolean">Allowed pick up or not.</ret>⤶ </rets> </function> <example> <description> Disallows picking up a weapon if player already has this weapon. ( Prevents ammo pickups from lying guns )⤶ Disallows picking up a weapon if player already has this weapon ( prevents ammo pickups from lying guns ). </description> <code> hook.Add( "PlayerCanPickupWeapon", "noDoublePickup", function( ply, wep ) if ( ply:HasWeapon( wep:GetClass() ) ) then return false end⤶ hook.Add( "PlayerCanPickupWeapon", "noDoublePickup", function( ply, weapon ) if ( ply:HasWeapon( weapon:GetClass() ) ) then⤶ return false⤶ end⤶ end ) </code> </example> ⤶ ⤶ <example>⤶ ⤶ <example>⤶ <description>Players can only pick up the HL2 Pistol.</description> <code> hook.Add( "PlayerCanPickupWeapon", "OnlyPistol", function( ply, wep ) return (wep:GetClass() == "weapon_pistol") hook.Add( "PlayerCanPickupWeapon", "OnlyPistol", function( ply, weapon ) return ( weapon:GetClass() == "weapon_pistol" ) end ) </code> ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>How you could give a player an alternate weapon to the one they picked up (such as an RPG Launcher rather than a pistol)</description>⤶ </example>⤶ ⤶ <example>⤶ <description>How you could give a player an alternate weapon to the one they picked up (such as an RPG Launcher rather than a pistol).</description>⤶ <code> hook.Add( "PlayerCanPickupWeapon", "NoPistolGiveFists", function( ply, wep ) if wep:GetClass() == "weapon_pistol" then -- if the weapon they are trying to pick up is 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 wep:Remove() -- remove the one they were trying to pick up weapon:Remove() -- remove the one they were trying to pick up return false -- don't give them a pistol end end ) </code> ⤶ </example></example>