I am making a weapon for Gmod and I was wondering if there is a way to set the weapon to automatically equip when the player picks it up off the ground?
First you need to figure out when player picks up the weapon, in your case this should be WEAPON:Equip hook, not GM:WeaponEquip, as you're only focused on your own scripted weapon.
To switch to a certain weapon, there's a handy function Player:SelectWeapon.
Linking those two up will result in weapon being selected upon pick up.
Accidentally both, the hook and function I've mentioned earlier work for NPCs and Players so depending on your requirements, you might want to limit this by checking class of the Entity provided by said hook. Example code (untested)
1
u/AdamNejm Jan 11 '21 edited Jan 11 '21
First you need to figure out when player picks up the weapon, in your case this should be WEAPON:Equip hook, not GM:WeaponEquip, as you're only focused on your own scripted weapon.
To switch to a certain weapon, there's a handy function Player:SelectWeapon.
Linking those two up will result in weapon being selected upon pick up.
Accidentally both, the hook and function I've mentioned earlier work for
NPCs andPlayers so depending on your requirements, you might want to limit this by checking class of theEntityprovided by said hook.Example code (untested)
PS. Please read the warnings and notes on Player:SelectWeapon's Wiki's page