Detecting who got hit by who

So I’m making a combat system for NPCs, and I want to check which players hit it, so I can give them the “assist”, or killed depending on if they hit it last. Is there reliable way to check that? I tried looping through the NPC’s parts or meshpart through a touchevent when their health got changed(HealthChanged:Connect(function())) but it’s not working the way it is intended.

1 Like

Well, this requires making a “tag” and placing inside the NPC’s humanoid and destroying it after a few seconds. So if you make custom weapons. In the part of your weapon script where you check if it hits an object. Do this: (Hit is a parameter you normally use in the “Touched” event which is the basepart that was touched.)

local DebrisService = game:GetService("Debris")
local Tag ="StringValue", hit.Parent.Humanoid)
Tag.Name == "KillTag"
Tag.Value = script.Parent.Parent.Parent.Name -- Script needs to be parented to the handle in your custom tool or the hitbox part of your tool.
DebrisService:AddItem(Tag, 2) -- Destroys the tag after 2 seconds.

-- Seperate script inside NPC parented to the NPC

   Iocal Humanoid = script.Parent.Humanoid
   local NumberOfTags = 0
   local Attackers = {} -- Table to store all attackers inside
   for i, v in pairs(script.Parent.Humanoid:GetChildren()) do
         if v.Name == "KillTag" then
               NumberOfTags = NumberOfTags + 1
               Table.Insert(Attackers, 1, v.Value)
   if #Attackers == 1 then -- If only 1 person attacked the npc then
        if game.Players:FindFirstChild(Attackers[1]) then -- Check if the player exists in the first place
              local Player = game.Players:FindFirstChild(Attackers[1])
              -- Do whatever code you want for the player now such as give them xp or coins
  else -- If more 0 or more than 1 players attacked the npc then
         for i, v in pairs(Attackers) do
               if game.Players:FindFirstChild(v) then -- Check if the player exists in the first place
                     local Player = game.Players:FindFirstChild(v)
                     -- Run your code for each player here such as give them xp or coins again 

I hope this helps you with your combat system!
I’m leaving the rest of the stuff to you since I am trying to not give a full script and want you to just use this to learn.

Quick fix: The first argument given by the .Touched event is the BasePart who touched the BasePart who the event was attached to.