> For the complete documentation index, see [llms.txt](https://wiki.redmodding.org/scripting-cyberpunk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.redmodding.org/scripting-cyberpunk/scripting/snippets-and-examples/entities/observing-weapon-hits.md).

# Observing weapon hits

Example code for observing a weapon hit

## Summary

Created: Oct 08 2025 by [mana vortex](mailto:undefined)\
Last documented update: Oct 08 2025 by [mana vortex](mailto:undefined)

This page shows a code sample for **reacting to hits on NPCs**.

## OnWeaponHit

{% tabs %}
{% tab title="REDScript" %}

```swift
// evt: https://nativedb.red4ext.com/gameHitEvent

@wrapMethod(NPCPuppet)
protected cb func OnHit(evt: ref<gameHitEvent>) -> Bool {
  wrappedMethod(evt);

  // check that the player was the one who hit this npc
  // https://nativedb.red4ext.com/AttackData
  let instigator: ref<GameObject> = evt.attackData.instigator;

  if IsDefined(instigator) && instigator.IsPlayer() {
    let record: ref<WeaponItem_Record> = evt.attackData.weapon.m_weaponRecord; // https://nativedb.red4ext.com/gamedataWeaponItem_Record
    let id = record.GetID();

    FTLog(s"weapon id: \(TDBID.ToStringDEBUG(id))");

    if Equals(id, t"Items.your_string_here") {
      FTLog(s"yay");
    }
  }
}
```

{% endtab %}

{% tab title="Lua" %}
{% code title="init.lua" %}

```lua
local function OnWeaponHit(this, evt) -- https://nativedb.red4ext.com/gameHitEvent
  
  -- check that the player was the one who hit this npc
  -- https://nativedb.red4ext.com/AttackData
  if not IsDefined(evt.attackData.instigator) or not evt.attackData.instigator:IsPlayer() then return end
  
  local record = evt.attackData.weapon.weaponRecord -- https://nativedb.red4ext.com/gamedataWeaponItem_Record
    
  local id = record:GetID().value -- make sure to stringify the TweakDBID so we can print and search

  print("weapon id: " .. id);

  if string.find(id, "Items.your_string_here") then
    print("yay")
  end    
end

registerForEvent("onInit", function()  
  ObserveAfter('NPCPuppet', 'OnHit', OnWeaponHit)
end)  
```

{% endcode %}

{% endtab %}
{% endtabs %}

### IsCritical

{% tabs %}
{% tab title="REDScript" %}

```swift
public func IsCritical(event: ref<gameHitEvent>) -> Bool {
  return IsDefined(event) && event.attackData.HasFlag(hitFlag.CriticalHit); 
}
```

{% endtab %}

{% tab title="Lua" %}

```lua
local function IsCritical(evt)
  return evt.attackData:HasFlag(hitFlag.CriticalHit)
end
```

{% endtab %}
{% endtabs %}

### IsKill

{% tabs %}
{% tab title="REDScript" %}

```swift
public func IsKill(event: ref<gameHitEvent>) -> Bool {
  return IsDefined(event) && event.attackData.HasFlag(hitFlag.Kill); 
}
```

{% endtab %}

{% tab title="Lua" %}

```lua
local function IsKill(evt)
  return evt.attackData:HasFlag(hitFlag.Kill)
end
```

{% endtab %}
{% endtabs %}
