Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to count how many times an individual NPC had been been killed?

I want to know how will I be able to count the amount of times an individual NPC has been killed.

For example let’s say I have two npcs in my game. Lex and Aaron. If you kill them, the kill counts go up towards them.

Would love to know how. Thanks

1 comment

0

you're being bit of a sussy baka

1 answer

✓ Accepted answer

So, I am unsure if this piece will work on a NPC, but Ik it works on a player. Humanoids have a ability to call a function when a user is dead. This is done through Humanoid.Died:Connect(). [Go here for more info on Humanoid.Died!]

Because of this, we can insert a int value inside the NPC and name it 'Deaths.'. Next, put a script inside the NPC and this will be the code you put in:

local Humanoid = script.Parent:FindFirstChild("Humanoid")
local Deaths = script.Parent:FindFirstChild("Deaths")
Humanoid.Died:Connect(function()
	Deaths.Value = Deaths.Value + 1
	print("Deaths.Value")
end)

First, it will find both the humanoid and the deaths value. It will then wait untill the NPC is dead, and then add 1 to the deaths value. It will then print the value so you know how many times it has been killed.

If this does not work, you can try use something like:

local Humanoid = script.Parent:FindFirstChild("Humanoid")
local Deaths = script.Parent:FindFirstChild("Deaths")
while wait(1) do
	if Humanoid.Health <= 0 then
		Deaths.Value = Deaths.Value + 1
		print("Deaths.Value")
	end
end

This is simply just the same, just it keeps checking every 1 second for if the health goes to 0, or somehow below.

Personally, I believe the first will be the most helpful, as it is not constantly running. But if that does not work, try the second one!

2 comments

0

that works but when it respawns it resets the value of the deaths, any idea how I can save it? thanks

0

Ah, create a folder in serverstorage, then have values in there for each npc.

This discussion is preserved as it was. New answers, comments, and votes are closed.