Hello. I am trying to detect if a player kills another player in a script. Once I know they have gotten a kill, it will increase a NumberValue.
How to detect if a player kills another player?
3 answers
Hello! I actually have no idea, but luckily a while back Roblox created a KOs and WOs leaderboard, so you can use that.
Sorry for no real answer, but I'm sure someone else can comment. Please don't accept this as the answer, as it isn't deserving of that. Here is the link to the model.
You'll probably have to check if Humanoid.Health == 0 after damage is dealt in the script you use to deal damage
You can tag a player when they're damaged by a tool. Let's say you have a sword. When a player touches your sword, it will make an ObjectValue inside the humanoid and its Value is yourself, but we will make it destroy after two seconds by using Debris:AddItem().
When a player dies, it will check if there's an ObjectValue inside their humanoid. And if it does, it will give the points to its Value (or the player who killed them).
The Classic Sword already has the tag script and some might have it too. But if your tool doesn't have a tag script and/or you're creating your custom tool, you can put this script in your tool. (Normal Script inside a Tool)
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local tool = script.Parent
local handle = tool.Handle
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
handle.Touched:Connect(function(hit)
local character = tool.Parent
local player = Players:GetPlayerFromCharacter(character)
local hitChar = hit.Parent
local hitPlr = Players:GetPlayerFromCharacter(hitChar)
if player and hitPlr then
if hitChar ~= character then
TagHumanoid(hitChar:FindFirstChildWhichIsA("Humanoid"), player)
end
end
end
And this is the script to detect if a player kills another player. (Normal Script inside ServerScriptService.)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
humanoid.Died:Connect(function()
local tag = humanoid:FindFirstChild("creator")
local killer = tag.Value
if tag and killer then
if killer:IsA("Player") then
-- increase a NumberValue
end
end
end)
end)
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
