Detect Who Killed NPC

Hello, good (evening, morning, night).

I am trying to create a quest system where if you kill 10 NPC’s you gain cash, but I am struggling trying to figure out how to make it so that I can detect which player kills an NPC. All help appreciated!

I assume that the player kills the npc with aa tool, then when the damage function is triggered somewhere in you games code, in that check if the NPC died after applying damage, if so get the character who attacked the npc then get its player by game.Players:GetPlayerByCharacter mathod.

I’m confused on that but the player does damage with a combat system (punches) it’s not a tool.

Add the checker in the function or code block that applies damage.

Assuming you have a combat system in place where it can damage the target.

-- your hit detection
local function onHit(target, plr)
    local player = plr.Name -- changeable to the player who hit
    NPC:SetAttribute("LastHitBy", player)
end


-- place script in NPC
local Players = game:GetService("Players");
NPC.Humanoid.Died:Once(function()
    local playerWhoKilled = NPC:GetAttribute("LastHitBy");
    Players[playerWhoKilled].leaderstats.Kills += 1;
end)

How this script works is that when the player hits an NPC, it sets the attribute of that NPC to the player name. When the NPC dies, it searches for the player’s name and adds a kill value. The location of the kill value is up to you and you can change it to your liking.

I hope this inspires you and helps you on your journey.

do something like:

if EnemyHit.Humanoid.Health <= Damage then

-- send a function of the kill to the server for the quest

end

Tried doing this;

local Players = game:GetService("Players");
local NPC = script.Parent

NPC.Humanoid.Died:Once(function()
	local playerWhoKilled = NPC:GetAttribute("LastHitBy");
	print(playerWhoKilled)
	playerWhoKilled.KillCount.Value += 1
end)

However it says Workspace.Characters. .PlayerKillCountGiver:7: attempt to index nil with 'Value'

and when I remove Value it still doesn’t work

That’s very interesting, could I see the KillCount object?

local killCount = Instance.new("IntValue")
		killCount.Name = "KillCount"
		killCount.Value = 0
		killCount.Parent = plr

Is the value in the player’s character or just the player?

Just the player. char char char

I see, if it’s in the player then you have to do:

local Players = game:GetService("Players");

NPC.Humanoid.Died:Once(function()
	local playerWhoKilled = NPC:GetAttribute("LastHitBy");
	print(playerWhoKilled)
	Players[playerWhoKilled].KillCount.Value += 1
end)

I hope this works.

KillCount is not a valid member of Player "Players.MRG_GROUPHOLDER"

however;

It’s set up like Players.MRG_GROUPHOLDER.KillCount

This is very unusual. Have you double checked that the value exists in the player?

Yes, I tried testing it with my main account and it doesn’t work either.

Screen Shot 2024-06-11 at 10.53.14 AM

Oh! It appears that the value should actually bi in the leaderstats folder, and not in the actual player!

In that case, try this:

local Players = game:GetService("Players");

NPC.Humanoid.Died:Once(function()
	local playerWhoKilled = NPC:GetAttribute("LastHitBy");
	print(playerWhoKilled)
	Players[playerWhoKilled].leaderstats.KillCount.Value += 1
end)

It’s not in the leaderstats, it’s in the actual Player. When I scripted to create the KillCount I made it’s parent be player.

You should place the value in a folder, and not in the actual player.

If you don’t like this idea, you could try attributes.

I just realized it might be because I am making KillCount client side… :sweat_smile:

1 Like

IT WORKS AFTER HOURS OF TRYING! Thanks so much :smiley:

1 Like