OnPlayerWeaponSwitch: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Fix example)
 
(16 intermediate revisions by 12 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
This event is triggered when a player switches weapons.
{{Server event}}
This event is triggered whenever a player's equipped weapon '''slot''' changes. This means [[giveWeapon]] and [[takeWeapon]] will trigger this event if the equipped slot is forced to change.  


==Syntax==  
==Parameters==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
void onPlayerWeaponSwitch ( int previousWeaponID, int currentWeaponID )
int previousWeaponID, int currentWeaponID
</syntaxhighlight>  
</syntaxhighlight>  


==Variables==
*'''previousWeaponID''': An [[int|integer]] representing the [[weapons|weapon]] that was switched from.
* The source of this event is the player who switched his weapon
*'''currentWeaponID''': An [[int|integer]] representing the [[weapons|weapon]] that was switched to.
*'''previousWeaponID''': An integer representing the weapon that was switched from
 
*'''currentWeaponID''': An integer representing the weapon that was switched to
==Source==
The [[event system#Event source|source]] of this event is the [[player]] that switched his weapon.
 
==Cancel effect==
If this event is [[Event system#Canceling|canceled]], then the player's weapon won't be switched.


==Example==  
==Example==  
This example disables use of the minigun
This example allows you to disable shoot ability for certain weapons, it should be noted that it will be more efficient if done on client-side.
 
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--add an event handler for onPlayerWeaponSwitch
local weaponsToBlock = {
addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), "weaponSwitchDisableMinigun" )
[38] = true, -- minigun
function weaponSwitchDisableMinigun ( previousWeaponID, currentWeaponID ) --when a player switches his weapon
}
if currentWeaponID == 38 then --if the weapon ID is minigun
 
    toggleControl ( source, "fire", false ) --disable the fire button
function onPlayerWeaponSwitch(previousWeaponID, currentWeaponID)
else --otherwise
local blockFire = (not weaponsToBlock[currentWeaponID]) -- reverse bool, true/worthy will give us false, and false/unworthy will give us true
    toggleControl ( source, "fire", true ) --enable it
 
toggleControl(source, "fire", blockFire) -- toggle player control to fire weapon
end
end
addEventHandler("onPlayerWeaponSwitch", root, onPlayerWeaponSwitch)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
{{See also/Server event|Player events}}
{{Event_functions}}

Latest revision as of 09:44, 16 November 2023

This event is triggered whenever a player's equipped weapon slot changes. This means giveWeapon and takeWeapon will trigger this event if the equipped slot is forced to change.

Parameters

int previousWeaponID, int currentWeaponID
  • previousWeaponID: An integer representing the weapon that was switched from.
  • currentWeaponID: An integer representing the weapon that was switched to.

Source

The source of this event is the player that switched his weapon.

Cancel effect

If this event is canceled, then the player's weapon won't be switched.

Example

This example allows you to disable shoot ability for certain weapons, it should be noted that it will be more efficient if done on client-side.

local weaponsToBlock = {
	[38] = true, -- minigun
}

function onPlayerWeaponSwitch(previousWeaponID, currentWeaponID)
	local blockFire = (not weaponsToBlock[currentWeaponID]) -- reverse bool, true/worthy will give us false, and false/unworthy will give us true

	toggleControl(source, "fire", blockFire) -- toggle player control to fire weapon
end
addEventHandler("onPlayerWeaponSwitch", root, onPlayerWeaponSwitch)

See Also

Player events


Event functions

Shared