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

Detecting if a player kills a player [closed]

Asked by
alibix123 175
11 years ago

I'm making a PvP game, and I have no idea where to start on detecting who killed a player.

Locked by JesseSong

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
5
Answered by
Unclear 1776 Moderation Voter
11 years ago

Many people use "creator tags" to go about doing this.

What they do is add a snippet of code inside the code that deals damage to a character; this is where a "tag" is created. Often, this tag is an ObjectValue with the player who dealt the damage (or the "killer") set as the Value property of the ObjectValue. For example, here's a snippet of code...

-- Assuming target is the player who is damaged
-- Assuming player is the player who is dealing damage
if target.Character ~= nil then
    if target.Character:FindFirstChild("Humanoid") then

        local tag = Instance.new("ObjectValue")
            tag.Name    = "killer"
            tag.Value   = player
            tag.Parent  = target.Character.Humanoid

        game.Debris:AddItem(tag, 5) -- Just to remove it eventually

        target.Character.Humanoid:TakeDamage(50) -- Or whatever amount of damage they're taking
    end
end

Using the Died event in Humanoid, we can do something when a player dies. You can either get the first tag that you find in the Humanoid or get all of them and deal with them accordingly (assisted kills?). Here's some example code...

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:connect(function()
            for key, value in pairs(humanoid:GetChildren()) do
                local killer = value.Value
                if killer ~= nil then
                    -- Do something with the killer
                end
            end
        end)
    end)
end)
0
It's kinda hard to understand at first glance, but I'll keep reading over this till I get it. Thanks! alibix123 175 — 11y
Ad