Still have questions? Join our Discord server and get real time help.
1

# Detecting if a player kills a player

alibix123 175
6 years ago

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

5
Unclear 1776
6 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)
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 — 6y