OnPlayerWeaponSwitch: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m ("Cancel effect" before "Example")
m (Replace to predefined variables)
Line 21: Line 21:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
WeaponID = {
local weaponID = {
[31] = true,
[31] = true,
[36] = true,
[36] = true,
Line 28: Line 28:


--add an event handler for onPlayerWeaponSwitch
--add an event handler for onPlayerWeaponSwitch
addEventHandler ( 'onPlayerWeaponSwitch', getRootElement ( ),
addEventHandler('onPlayerWeaponSwitch', root, function(prevWeapon, curWeapon)
function ( previousWeaponID, currentWeaponID )
if weaponID[currentWeaponID] then
if ( WeaponID[currentWeaponID] ) then
toggleControl(source, 'fire', false) -- disable the fire button
toggleControl ( source, 'fire', false ) --disable the fire button
else
else
toggleControl(source, 'fire', true) -- enable it
toggleControl ( source, 'fire', true ) --enable it
end
end
end
)
end)
</syntaxhighlight>
</syntaxhighlight>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Revision as of 20:11, 14 June 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 disables use of the minigun upon switch. It should be noted that this can be done more efficiently clientside.

local weaponID = {
	[31] = true,
	[36] = true,
	[38] = true,
}

--add an event handler for onPlayerWeaponSwitch
addEventHandler('onPlayerWeaponSwitch', root, function(prevWeapon, curWeapon)
	if weaponID[currentWeaponID] then
		toggleControl(source, 'fire', false) -- disable the fire button
	else
		toggleControl(source, 'fire', true) -- enable it
	end
end)

See Also

Player events


Event functions

Shared