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

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

Asked by 3 years ago
Edited 3 years ago

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

0
So, what exactly is your problem? DesertusX 435 — 3y
0
I'm trying to detect the death of a bot by a player so it can add a kill to a leaderboard. User#35818 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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.

0
Forgot to add: you might need to make :WaitForChild() instead of :FindFirstChild() IAmNotTheReal_MePipe 418 — 3y
0
Thank you! It worked. User#35818 0 — 3y
Ad

Answer this question