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

Detecting if a player kills a player

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

1 answer

✓ Accepted answer

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)

1 comment

0

It's kinda hard to understand at first glance, but I'll keep reading over this till I get it. Thanks!

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