Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago
Edited 1 year ago

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

0
you're being bit of a sussy baka raid6n 2196 — 1y

1 answer

Log in to vote
-1
Answered by 1 year ago
Edited 1 year ago

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!

0
that works but when it respawns it resets the value of the deaths, any idea how I can save it? thanks Aliamasion 4 — 1y
0
Ah, create a folder in serverstorage, then have values in there for each npc. Jay123abc2 241 — 1y
Ad

Answer this question