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
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!