OnClientPlayerWeaponFire: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(25 intermediate revisions by 15 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 does not trigger for projectiles, melee weapons, or camera.
 
{{Note|This event is only triggered for players that are streamed in}}
==Syntax==  
{{Note|This does not trigger for projectiles, melee weapons, or camera.}}
==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 making a shot.
==Parameters==
*'''ammo''': an [[int]] amount of ammo left for this weapon type.
*The '''source''' of this event refers to the player who made a shot.
*'''ammoInClip''': an [[int]] amount of ammo left for this weapon type in clip.
*'''weapon''':  a [[weapons|weapon]] [[integer]] of a weapon used.
*'''ammo''': an [[integer]] ammount of ammo left for this weapon type.
*'''ammoInClip''': an [[integer]] ammount of ammo left for this weapon type in clip.
*'''hitX''', '''hitY''', '''hitZ''': [[float]] world coordinates representing a hit point.
*'''hitX''', '''hitY''', '''hitZ''': [[float]] world coordinates representing a 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''', '''startY''', '''startZ''': [[float]] world coordinates representing the start of the bullet. Note: This is not the gun muzzle.
}}
==Source==
The [[event system#Event source|source]] of this event is the streamed in [[player]] who fired the weapon.


==Example==  
==Example==  
This example shows player a warning if he hits any other player with minigun.
This example implements custom gunshot sounds.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- trigger the event every time player shots
function playGunfireSound(weaponID)
local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition(source)
local dim = getElementDimension(source)
local int = getElementInterior(source)
setAmbientSoundEnabled ("gunfire", false)
 
local weaponSounds = {
[22] = "sounds/weap/colt45.ogg",
[23] = "sounds/weap/silenced.ogg",
[24] = "sounds/weap/deagle.ogg",
[25] = "sounds/weap/shotgun.ogg",
[26] = "sounds/weap/sawed-off.ogg",
[27] = "sounds/weap/combat shotgun.ogg",
[28] = "sounds/weap/uzi.ogg",
[30] = "sounds/weap/ak-47.ogg",
[31] = "sounds/weap/m4.ogg",
[32] = "sounds/weap/tec9.ogg",
[34] = "sounds/weap/sniper.ogg",
}
 
if weaponSounds[weaponID] then
sound = playSound3D(weaponSounds[weaponID], muzzleX, muzzleY, muzzleZ)
setSoundMaxDistance(sound, 90)
setElementDimension(sound, dim)
setElementInterior(sound, int)
setSoundVolume(sound, 0.6)
end
end
addEventHandler("onClientPlayerWeaponFire", root, playGunfireSound)
</syntaxhighlight>
 
This example sends a warning to the local player if they shoot another player with a minigun.
<syntaxhighlight lang="lua">
--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", getLocalPlayer(), 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==
==Issues==
{{Projectile functions}}
{{Issues|
{{Issue|7459|[Fixed in 1.3.1-4916] onClientPlayerWeaponFire hitElement being nil sometimes with shotgun}}
}}
 
==See Also==
===Client player events===
{{Client_player_events}}
===Client event functions===
{{Client_event_functions}}

Revision as of 02:56, 14 October 2018

This event is called when a player fires a weapon. This does not trigger for projectiles, melee weapons, or camera.

[[{{{image}}}|link=|]] Note: This event is only triggered for players that are streamed in
[[{{{image}}}|link=|]] Note: This does not trigger for projectiles, melee weapons, or camera.

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 making 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, hitY, hitZ: float world coordinates representing a hit point.
  • hitElement: an element which was hit by a shot.
  • startX, startY, startZ: float world coordinates representing the start of the bullet. Note: This is not the gun muzzle.

Source

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

Example

This example implements custom gunshot sounds.

function playGunfireSound(weaponID)
	local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition(source)
	local dim = getElementDimension(source)
	local int = getElementInterior(source)
	setAmbientSoundEnabled ("gunfire", false)

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

	if weaponSounds[weaponID] then
		sound = playSound3D(weaponSounds[weaponID], muzzleX, muzzleY, muzzleZ)
		setSoundMaxDistance(sound, 90)
		setElementDimension(sound, dim)
		setElementInterior(sound, int)
		setSoundVolume(sound, 0.6)
	end
end
addEventHandler("onClientPlayerWeaponFire", root, playGunfireSound)

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", getLocalPlayer(), 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)

Issues

Issue ID Description
#7459 [Fixed in 1.3.1-4916] onClientPlayerWeaponFire hitElement being nil sometimes with shotgun

See Also

Client player events


Client event functions