OnClientPlayerWeaponFire: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
(31 intermediate revisions by 18 users not shown)
Line 1: Line 1:
{{Client event}}
__NOTOC__  
__NOTOC__  
This event is called when player shoots a weapon.
This event is called when a player fires a weapon.  This event does not trigger for melee weapons. Projectile weapons or the camera will only trigger the event if fired by the local player.
 
{{Note|This event is only triggered for players that are streamed in}}
==Syntax==  
{{Note|This does not trigger for any player's melee weapons or for remote player's projectile weapons or cameras}}
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
void onClientPlayerWeaponFire ( int weapon, int ammo, int ammoInClip, float hitX, float hitY, float hitZ, element hitElement )
int weapon, int ammo, int ammoInClip, float hitX, float hitY, float hitZ, element hitElement, float startX, float startY, float startZ
</syntaxhighlight>  
</syntaxhighlight>  
 
*'''weapon''':  an [[int]] representing [[weapons|weapon]] used for firing a shot.
==Parameters==
*'''ammo''': an [[int]] amount of ammo left for this weapon type.
*'''weapon''':  an [[int]] representing [[weapons|weapon]] used for making a shot.
*'''ammoInClip''': an [[int]] amount of ammo left for this weapon type in clip.
*'''ammo''': an [[int]] ammount of ammo left for this weapon type.
*'''hitX''': [[float]] world X coordinate representing the hit point.
*'''ammoInClip''': an [[int]] ammount of ammo left for this weapon type in clip.
*'''hitY''': [[float]] world Y coordinate representing the hit point.
*'''hitX''', '''hitY''', '''hitZ''': [[float]] world coordinates representing a hit point.
*'''hitZ''': [[float]] world Z coordinate representing the hit point.
*'''hitElement''': an [[element]] which was hit by a shot.
*'''hitElement''': an [[element]] which was hit by a shot.
{{New feature/item|3.0131|1.3.1|4311|
*'''startX''': [[float]] world X coordinate representing the start of the bullet. Note: This is not the gun muzzle.
*'''startY''': [[float]] world Y coordinate representing the start of the bullet.
*'''startZ''': [[float]] world Z coordinate representing the start of the bullet.
}}


==Source==
==Source==
The [[event system#Event source|source]] of this event is the player who fired the weapon.
The [[event system#Event source|source]] of this event is the streamed in [[player]] who fired the weapon.
 
==Example==
This example implements custom gunshot sounds.
<syntaxhighlight lang="lua">
local playerWeaponSounds = {
[22] = "sounds/weap/colt45.wav",
[23] = "sounds/weap/silenced.wav",
[24] = "sounds/weap/deagle.wav",
[25] = "sounds/weap/shotgun.wav",
[26] = "sounds/weap/sawed-off.wav",
[27] = "sounds/weap/combat shotgun.wav",
[28] = "sounds/weap/uzi.wav",
[30] = "sounds/weap/ak-47.wav",
[31] = "sounds/weap/m4.wav",
[32] = "sounds/weap/tec9.wav",
[34] = "sounds/weap/sniper.wav",
}
 
local function playCustomWeaponSound(weaponID)
local playerWeaponSoundPath = playerWeaponSounds[weaponID]
 
if (not playerWeaponSoundPath) then
return false
end
 
local playerMuzzleX, playerMuzzleY, playerMuzzleZ = getPedWeaponMuzzlePosition(source)
local playerWeaponSound = playSound3D(playerWeaponSoundPath, playerMuzzleX, playerMuzzleY, playerMuzzleZ)


if (not playerWeaponSound) then
return false
end


==Example==
setSoundMaxDistance(playerWeaponSound, 90)
This example shows player a warning if he hits any other player with minigun.
setSoundVolume(playerWeaponSound, 0.6)
end
addEventHandler("onClientPlayerWeaponFire", root, playCustomWeaponSound)
</syntaxhighlight>
 
This example sends a warning to the local player if they shoot another player with a minigun.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- trigger the event every time player shots
--First, we create a function for the event handler to use.
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
     if weapon == 38 and getElementType(hitElement)=="player" then -- if player shots from minigun and he hits another player...
     if weapon == 38 and getElementType(hitElement)=="player" then -- If the player shoots with a minigun, and hits another player...
         outputChatBox ( "Don't kill people with minigun, it's lame!", source ) -- then we output him a warning
         outputChatBox ( "Don't kill people with minigun, it's lame!", 255, 0, 0 ) -- We output a warning to him.
    end
end
-- Add this as a handler so that the function will be triggered every time the local player fires.
addEventHandler ( "onClientPlayerWeaponFire", localPlayer, onClientPlayerWeaponFireFunc )
</syntaxhighlight>
 
This example makes the Shotgun fire explosive rounds.
<syntaxhighlight lang="lua">
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
    if (weapon == 25) then -- If the player shoots with a shotgun
        createExplosion(hitX, hitY, hitZ, 12, true, 0, true) -- Creates a tiny explosion where the bullet hit.
     end
     end
end
end
-- don't forget to add the onClientPlayerWeaponFireFunc function as a handler for onClientPlayerWeaponFire
-- Add this as a handler so that the function will be triggered every time a player fires.
addEventHandler ( "onPlayerSpawn", getRootElement(), onClientPlayerWeaponFireFunc )
addEventHandler("onClientPlayerWeaponFire", root, onClientPlayerWeaponFireFunc)
</syntaxhighlight>
</syntaxhighlight>
==See Also==
===Client player events===
{{Client_player_events}}
===Client event functions===
{{Client_event_functions}}

Latest revision as of 05:19, 5 June 2025

This event is called when a player fires a weapon. This event does not trigger for melee weapons. Projectile weapons or the camera will only trigger the event if fired by the local player.

[[{{{image}}}|link=|]] Note: This event is only triggered for players that are streamed in
[[{{{image}}}|link=|]] Note: This does not trigger for any player's melee weapons or for remote player's projectile weapons or cameras

Parameters

int weapon, int ammo, int ammoInClip, float hitX, float hitY, float hitZ, element hitElement, float startX, float startY, float startZ
  • weapon: an int representing weapon used for firing a shot.
  • ammo: an int amount of ammo left for this weapon type.
  • ammoInClip: an int amount of ammo left for this weapon type in clip.
  • hitX: float world X coordinate representing the hit point.
  • hitY: float world Y coordinate representing the hit point.
  • hitZ: float world Z coordinate representing the hit point.
  • hitElement: an element which was hit by a shot.
  • startX: float world X coordinate representing the start of the bullet. Note: This is not the gun muzzle.
  • startY: float world Y coordinate representing the start of the bullet.
  • startZ: float world Z coordinate representing the start of the bullet.

Source

The source of this event is the streamed in player who fired the weapon.

Example

This example implements custom gunshot sounds.

local playerWeaponSounds = {
	[22] = "sounds/weap/colt45.wav",
	[23] = "sounds/weap/silenced.wav",
	[24] = "sounds/weap/deagle.wav",
	[25] = "sounds/weap/shotgun.wav",
	[26] = "sounds/weap/sawed-off.wav",
	[27] = "sounds/weap/combat shotgun.wav",
	[28] = "sounds/weap/uzi.wav",
	[30] = "sounds/weap/ak-47.wav",
	[31] = "sounds/weap/m4.wav",
	[32] = "sounds/weap/tec9.wav",
	[34] = "sounds/weap/sniper.wav",
}

local function playCustomWeaponSound(weaponID)
	local playerWeaponSoundPath = playerWeaponSounds[weaponID]

	if (not playerWeaponSoundPath) then
		return false
	end

	local playerMuzzleX, playerMuzzleY, playerMuzzleZ = getPedWeaponMuzzlePosition(source)
	local playerWeaponSound = playSound3D(playerWeaponSoundPath, playerMuzzleX, playerMuzzleY, playerMuzzleZ)

	if (not playerWeaponSound) then
		return false
	end

	setSoundMaxDistance(playerWeaponSound, 90)
	setSoundVolume(playerWeaponSound, 0.6)
end
addEventHandler("onClientPlayerWeaponFire", root, playCustomWeaponSound)

This example sends a warning to the local player if they shoot another player with a minigun.

--First, we create a function for the event handler to use.
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
    if weapon == 38 and getElementType(hitElement)=="player" then -- If the player shoots with a minigun, and hits another player...
         outputChatBox ( "Don't kill people with minigun, it's lame!", 255, 0, 0 ) -- We output a warning to him.
    end
end
-- Add this as a handler so that the function will be triggered every time the local player fires.
addEventHandler ( "onClientPlayerWeaponFire", localPlayer, onClientPlayerWeaponFireFunc )

This example makes the Shotgun fire explosive rounds.

function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
    if (weapon == 25) then -- If the player shoots with a shotgun
        createExplosion(hitX, hitY, hitZ, 12, true, 0, true) -- Creates a tiny explosion where the bullet hit.
    end
end
-- Add this as a handler so that the function will be triggered every time a player fires.
addEventHandler("onClientPlayerWeaponFire", root, onClientPlayerWeaponFireFunc)

See Also

Client player events


Client event functions