Zum Inhalt springen

Game-Damage-Events

Versteh den Payload von gameEventTriggered / CEventNetworkEntityDamage und häng saubere onPlayerKilled-, onPlayerDied- und Fahrzeug-zerstört-Events in deine eigene Resource.

Dieses Tool und seine Hinweise sind vorerst auf Englisch.

ReferenzenEvents, DokuZur Doku

damage:onPlayerDied

Local player· client

The local player died, whatever the cause. Fires once per death, even when the game skipped the damage event (a watchdog thread checks IsEntityDead).

#ArgTypeDescription
1causestring"player", "ped", "vehicle", "suicide" or "environment".
2killerServerIdnumber|nilServer id of the killer when cause is "player".
3weaponHashhashWeapon or cause of damage.
4killerEntityentityKiller ped or vehicle handle, 0 when none.
lua
AddEventHandler("damage:onPlayerDied", function(cause, killerServerId, weaponHash, killerEntity)
    print("onPlayerDied", cause, killerServerId, weaponHash, killerEntity)
end)

damage:onPlayerKilledByPlayer

Local player· client

The local player was killed by another player (also when run over by a car a player drives).

#ArgTypeDescription
1killerServerIdnumberServer id of the killer.
2weaponHashhashWeapon or cause of damage.
3isMeleebooleanWeapon is in the melee or unarmed group.
4isHeadshotbooleanLast damaged bone was the head (SKEL_Head).
5distancenumberDistance to the killer in metres.
lua
AddEventHandler("damage:onPlayerKilledByPlayer", function(killerServerId, weaponHash, isMelee, isHeadshot, distance)
    print("onPlayerKilledByPlayer", killerServerId, weaponHash, isMelee, isHeadshot, distance)
end)

damage:onPlayerKilledByPed

Local player· client

The local player was killed by an NPC.

#ArgTypeDescription
1killerPedentityThe NPC ped.
2weaponHashhashWeapon or cause of damage.
3isMeleebooleanWeapon is in the melee or unarmed group.
lua
AddEventHandler("damage:onPlayerKilledByPed", function(killerPed, weaponHash, isMelee)
    print("onPlayerKilledByPed", killerPed, weaponHash, isMelee)
end)

damage:onPlayerKilledByVehicle

Local player· client

The local player was run over or crushed by a vehicle with no player driver.

#ArgTypeDescription
1vehicleentityThe vehicle.
2weaponHashhashWeapon or cause of damage.
lua
AddEventHandler("damage:onPlayerKilledByVehicle", function(vehicle, weaponHash)
    print("onPlayerKilledByVehicle", vehicle, weaponHash)
end)

damage:onPlayerKilled

Local player· client

The local player killed another player (killer side). Use it for kill feeds, streaks and rewards.

#ArgTypeDescription
1victimServerIdnumberServer id of the victim.
2weaponHashhashWeapon or cause of damage.
3isMeleebooleanWeapon is in the melee or unarmed group.
4isHeadshotbooleanLast damaged bone was the head (SKEL_Head).
5distancenumberDistance to the victim in metres.
lua
AddEventHandler("damage:onPlayerKilled", function(victimServerId, weaponHash, isMelee, isHeadshot, distance)
    print("onPlayerKilled", victimServerId, weaponHash, isMelee, isHeadshot, distance)
end)

damage:onPlayerDamaged

Local player· client· frequent

The local player took damage and survived.

#ArgTypeDescription
1attackerentityAttacker ped or vehicle, 0 / -1 when none.
2weaponHashhashWeapon or cause of damage.
3isMeleebooleanWeapon is in the melee or unarmed group.
4attackerServerIdnumber|nilServer id when the attacker is a player.
lua
AddEventHandler("damage:onPlayerDamaged", function(attacker, weaponHash, isMelee, attackerServerId)
    print("onPlayerDamaged", attacker, weaponHash, isMelee, attackerServerId)
end)

damage:onPedKilledByPlayer

Peds· client

The local player killed an NPC ped.

#ArgTypeDescription
1pedentityThe dead ped.
2weaponHashhashWeapon or cause of damage.
3isMeleebooleanWeapon is in the melee or unarmed group.
4isHeadshotbooleanLast damaged bone was the head (SKEL_Head).
lua
AddEventHandler("damage:onPedKilledByPlayer", function(ped, weaponHash, isMelee, isHeadshot)
    print("onPedKilledByPlayer", ped, weaponHash, isMelee, isHeadshot)
end)

damage:onPedDied

Peds· client· every client

An NPC ped died and the local player was not the killer.

#ArgTypeDescription
1pedentityThe dead ped.
2attackerentityKiller entity, 0 / -1 when none.
3weaponHashhashWeapon or cause of damage.
lua
AddEventHandler("damage:onPedDied", function(ped, attacker, weaponHash)
    print("onPedDied", ped, attacker, weaponHash)
end)

damage:onVehicleDestroyed

Vehicles· client· every client

A vehicle was destroyed (blew up or was wrecked).

#ArgTypeDescription
1vehicleentityThe vehicle.
2attackerentityEntity that destroyed it.
3weaponHashhashWeapon or cause of damage.
4byLocalPlayerbooleanThe local player (or their vehicle) did it.
lua
AddEventHandler("damage:onVehicleDestroyed", function(vehicle, attacker, weaponHash, byLocalPlayer)
    print("onVehicleDestroyed", vehicle, attacker, weaponHash, byLocalPlayer)
end)

damage:onVehicleDamaged

Vehicles· client· every client· frequent

A vehicle took damage but was not destroyed.

#ArgTypeDescription
1vehicleentityThe vehicle.
2attackerentityEntity that damaged it.
3weaponHashhashWeapon or cause of damage.
lua
AddEventHandler("damage:onVehicleDamaged", function(vehicle, attacker, weaponHash)
    print("onVehicleDamaged", vehicle, attacker, weaponHash)
end)

damage:onEntityDamaged

Raw· client· every client· frequent

Every CEventNetworkEntityDamage, untouched. Handy for debugging new builds.

#ArgTypeDescription
1victimentityargs[1]
2attackerentityargs[2]
3weaponHashhashWeapon or cause of damage.
4fatalbooleanargs[6] == 1
5argstableThe raw args table.
lua
AddEventHandler("damage:onEntityDamaged", function(victim, attacker, weaponHash, fatal, args)
    print("onEntityDamaged", victim, attacker, weaponHash, fatal, args)
end)

gameEventTriggered and CEventNetworkEntityDamage

FiveM forwards low-level game events to scripts through the client event gameEventTriggered. Every time a networked entity takes damage, the game raises CEventNetworkEntityDamage and you get its payload as a flat array of integers:

AddEventHandler("gameEventTriggered", function(name, args)
    if name == "CEventNetworkEntityDamage" then
        local victim   = args[1]
        local attacker = args[2]
        local fatal    = args[6] == 1
        local weapon   = args[7]
    end
end)

The array has no names and its layout changed over time: game build 2060 and 2189 each inserted an extra value before fatal, which is why old snippets read args[4] and args[5] and silently break. Victim, attacker, fatal and weapon are reliable on current builds. The values after the weapon move around, so the generated code works out melee from the weapon group and headshots fromGetPedLastDamageBone instead of trusting those slots.

Why wrap it in cleaner events

  • One handler does the parsing, every other script just listens to damage:onPlayerKilledByPlayer and friends.
  • The event fires on every client that knows the victim, not only on the victim and the killer. The generated events filter on the local player where it matters, so a kill is only reported once.
  • Deaths are de-duplicated: shooting a body again does not fire a second death, and a small watchdog catches deaths that never raised a damage event (fall damage on some builds, SetEntityHealth(ped, 0)).

Server relay

The server does not get CEventNetworkEntityDamage. The relay lets the victim report its own death, which is the least abusable direction: a cheater can only lie about their own death. The server side still checks the payload type, rate limits reports, verifies the killer is another connected player and (with OneSync) drops kills from further than the max distance. Treat it as a kill feed source, not as proof for bans.

Credits

The idea of turning the raw damage event into named events comes from Vespura'sDamageEvents resource (C#, DamageEvents:PedKilledByPlayer etc.). The Lua generated here is a separate implementation written for current game builds.