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

01-- Assuming target is the player who is damaged
02-- Assuming player is the player who is dealing damage
03if target.Character ~= nil then
04    if target.Character:FindFirstChild("Humanoid") then
05 
06        local tag = Instance.new("ObjectValue")
07            tag.Name    = "killer"
08            tag.Value   = player
09            tag.Parent  = target.Character.Humanoid
10 
11        game.Debris:AddItem(tag, 5) -- Just to remove it eventually
12 
13        target.Character.Humanoid:TakeDamage(50) -- Or whatever amount of damage they're taking
14    end
15end

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

01game.Players.PlayerAdded:connect(function(player)
02    player.CharacterAdded:connect(function(character)
03        local humanoid = character:WaitForChild("Humanoid")
04        humanoid.Died:connect(function()
05            for key, value in pairs(humanoid:GetChildren()) do
06                local killer = value.Value
07                if killer ~= nil then
08                    -- Do something with the killer
09                end
10            end
11        end)
12    end)
13end)
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