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

How to detect a death from a non-player humanoid?

[REPOST]

I am making a swordfighting game which has AI that can also fight. I have made leaderstats for Deaths, and also Kills (player to player kills). But I would also like to make it so it adds one kill to the leaderboard when you kill a bot (player to bot kills).

This is my code currently (mostly from tutorials + free models):

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"


Players.PlayerAdded:connect(function(Player)
	wait(1)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
	Player.CharacterAdded:connect(function(Character)
		Deaths.Value = Deaths.Value + 1
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return
					end
				end
			end)
		end
	end)
end)

Thank you!

2 comments

0

So, what exactly is your problem?

0

I'm trying to detect the death of a bot by a player so it can add a kill to a leaderboard.

1 answer

✓ Accepted answer

You would add this function in ServerScriptService:

game.Workspace:FindFirstChild("NPCName").Humanoid.Died:Connect(function()
     --Run Code
end)

I think this script is pretty descriptive already.

2 comments

0

Forgot to add: you might need to make :WaitForChild() instead of :FindFirstChild()

0

Thank you! It worked.

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