Player/SelectWeapon

From Garry's Mod
Jump to: navigation, search
 Player:SelectWeapon( )

Contents

Description

Sets the active weapon of the player by its class name.

WARNING

This will switch the weapon out of prediction, causing delay on the client and WEAPON:Deploy to be called out of prediction. Try using CUserCmd:SelectWeapon or input.SelectWeapon, instead.

NOTE

This will trigger the weapon switch event and associated animations. To switch weapons silently, use Player:SetActiveWeapon.

Arguments

string className

The class name of the weapon to switch to.

If the player doesn't have the specified weapon, nothing will happen. You can use Player:Give to give the weapon first.

Examples

Example

Force the player to switch to toolgun

Entity(1):SelectWeapon("gmod_tool")


Examples

Example

Selects a random weapon from the player's inventory and switches to it.

local weapons = Entity( 1 ):GetWeapons()
local weapon = weapons[ math.random( #weapons ) ]

Entity( 1 ):SelectWeapon( weapon:GetClass() )


Personal tools
Navigation