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