Observing weapon hits

Example code for observing a weapon hit

Summary

Created: Oct 08 2025 by mana vortex Last documented update: Oct 08 2025 by mana vortex

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

OnWeaponHit

init.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)  

IsCritical

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

IsKill

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

Last updated