Prevent Weapon Switching When Weapon is Picked Up?
3 replies, posted
Im trying to make a swep that once the weapon is picked up, the player can't switch to other weapons. They must first drop that weapon to switch to their other weapons.
Anyone know of this is possible? What's the code?
I don't know of any [i]clean[/i] ways this can be done, but you can use the [i]PlayerSwitchWeapon[/i] hook, which is called just after a weapon switch, to select the previous weapon if it was a certain class.
e.g.
[lua]hook.Add("PlayerSwitchWeapon", "PreventWeaponSwitch", function(ply, old)
--GAMEMODE:PlayerSwitchWeapon(ply, old) to override.
if !IsValid(old) || !IsValid(ply) then return end
local oc = old:GetClass()
if oc == "your_weapon_class" then ply:SelectWeapon(oc) end
end)[/lua]
You'd then enable [i]AutoSwitchTo[/i] and disable [i]AutoSwitchFrom[/i] in your SWEP information, so that the weapon is selected immediately after pickup.
[code]function SWEP:Equip( pl )
pl:SelectWeapon( self:GetClass( ) )
end
function SWEP:Holster( )
self.Owner:GetViewModel( ):RemoveEffects( EF_NODRAW )
return false
end[/code]
You'll need to make the player drop the weapon on your own, but that should do the trick.
Ah cool. Thanks :D
Sorry, you need to Log In to post a reply to this thread.