How to check who killed the NPC?

Hey, I’m looking on how to check who killed an NPC.

I know that you have to add an attribute and whenever you damage the player through the tool you change the attribute to the players username. However, I am using ACS 1.75, (gun system) and it’s near impossible to edit the code. Does anyone know how, whenever the gun deals damage, I can change the attribute?

Thanks!

6 Likes
local npc = script.Parent
local gun = game.Workspace.Gun

local function onDamage(damageInfo)
    local player = damageInfo.Player
    if player then
        npc:SetAttribute("Killer", player.Name)
    end
end

gun.OnDamage:Connect(onDamage)

2 Likes

Where exactly should I put this?

3 Likes

This is scripted as if the script was under the NPC model, but you can change the variables value to where ever the npc is, & put the script in serverscriptservice

1 Like

Sorry I meant to ask: What is “local gun?” If you’re referring to the weapon the player uses, it’s in their backpack and they have multiple so I wouldn’t be able to call out the instance unless I list every single one each player owns.

2 Likes

You can do a tool.equipped function on all tools and unequipped to always know which ones equipped, or check for a tool under “Player.Character” (when a tool is equipped it will always be parented under the character), then change the variable to be set to that if you want it to target the gun thats equipped

1 Like

If you insist on not using Attributes, you can resort to a somewhat hacky solution of using Tags.

Find the part in the script where it applies damage to the Humanoid of the Character that got shot.
Then, insert this to add a tag to the Humanoid of who last applied the damage.

if #humanoid:GetTags() >= 0 then --//if pre-existing tags
	humanoid:RemoveTag(humanoid:GetTags()[1]) --//delete existing
	humanoid:AddTag(tostring(playerShooting)) --//add tag with the player that applied damage
else
	humanoid:AddTag(tostring(playerShooting))
end

If necessary, you can attach this script to the NPC or apply it to the script/module that handles the NPC to address who killed it.

humanoid.Died:Connect(function()
	warn(humanoid.Parent, "got killed by", humanoid:GetTags()[1])
end)
1 Like

That’s the thing, I don’t know where the “damage” function is. I do want to use attributes but I was wondering if there’s a workaround.

1 Like

Bumping, I still need help.

Nevermind, I figured it out. I scanned the ACS_Server code and was able to find where the gun deals damage. Thanks all!