PvP : How to Track Kills

Hi guys!

When making a Player versus Player game, you will eventually have to track kills. Today I decided to make a quick tutorial on how to know which player killed another player.

Bonus: We will make a leaderboard that will get updated when someone gets killed.

This process can be applied to any kind of weapons as long as you follow the logic behind the tutorial.

What is needed

  1. The classic Roblox sword(Can be found in the toolbox)
  2. Basic scripting knowledge
  3. Fun

So how will this work
Basically, what we are going to do is: When someone manages to kill another player, an object value will be created and the value will be the “killer”. Then, when someone dies, we will check the value to know who the killer is and add a kill to the leaderboard.

First
Firslty add the sword to the startpack and add a script in ServerScriptService.
b68b3023fa9556b14aeee573e3590c25
5520a1e9d11297ce303dadbf532f8853

Second
Now we want to create the leaderboard(will be called “leaderstat” in the script).
Open the script in ServerScriptService.

local Players = game:GetService("Players")

local function playerJoin(plr)
	local leaderStats = Instance.new("Folder")
	leaderStats.Name = "leaderstats"
	leaderStats.Parent = plr

	local kills = Instance.new("IntValue")
	kills.Name =  "kills"
	kills.Value = 0
	kills.Parent = leaderStats

end

Players.PlayerAdded:Connect(playerJoin)

What are we doing in the script?: When a player joins, we make a folder called leaderstats and add values in it. This will make a leaderboard appear in game, make sure to parent the leaderstat to the player.
822379fc093478cca7b0baea072fe71b

Third
Now we need to open the script inside the sword called “SwordScript”. We will add some code so that an Object Value is created when we hit a player.


In this image, I added ;

if player:FindFirstChild("WhoHit") then
		player.WhoHit.Value = Player
		print("Yes")
	else
		
		print("no")
		local whoHit = Instance.new("ObjectValue")
		whoHit.Name = "WhoHit"
		whoHit.Value = Player
		whoHit.Parent = player
				
end

This code check if the Object Value that I called WhoHit (I’m a genius name generator) exist, if it exist we are going to replace the value by the player who just hit another one, if it does not exist we are going to create a new one and parent it to the player who got hit. Player = the player who is hitting another one player = the player who is getting hit. I know it’s a bit confusing but the classic sword script is using these names. Why are we putting this script in the Blow function? Because it is where, in the script it checks for hits.

Fourth
Now we need need to check when someone dies who killed him. For that, we will check the Object Value and add a kill to the player who killed the person who just died. Remember that the killer is stored in the Object Value.

plr.CharacterAdded:Connect(function(char)
	local humanoid = char.Humanoid
		
	humanoid.Died:Connect(function()
			
	if plr:FindFirstChild("WhoHit") then
		local killer = plr.WhoHit.Value
		killer.leaderstats.kills.Value = killer.leaderstats.kills.Value + 1
		plr.WhoHit:Destroy()
	else
		print("No one killed "..plr.Name)
	end
			
end)


Finally
Now we just need to combine part 2 (Second) and part 4 (Fourth).

local Players = game:GetService("Players")

local function playerJoin(plr)
	local leaderStats = Instance.new("Folder")
	leaderStats.Name = "leaderstats"
	leaderStats.Parent = plr
	
	local kills = Instance.new("IntValue")
	kills.Name =  "kills"
	kills.Value = 0
	kills.Parent = leaderStats
	
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char.Humanoid
		
		humanoid.Died:Connect(function()
			
		if plr:FindFirstChild("WhoHit") then
			local killer = plr.WhoHit.Value
			killer.leaderstats.kills.Value = killer.leaderstats.kills.Value + 1
			plr.WhoHit:Destroy()
		else
			print("No one killed "..plr.Name)
		end
			
		end)
		
		
	end)
	
	
end

Players.PlayerAdded:Connect(playerJoin)

The script that you created at the beginning should be replaced by the script above.

Conclusion

Now you know 1 way of tracking player kills.

This method would be great if combined with a bleeding system: You hit someone he is not dead but is bleeding, some moment later, he dies and the killer is still credited with the kill.
If you want to apply this method to other weapons don’t just copy the script apply the concept because if you copy the script it won’t work with weapons that don’t work exactly as the “Classe sword”.

Note that this script does not take in factor healing.
If someone regenerates all his health and dies the last person who hit him will be credited with his death. To fix this all you need to do if check if the player is full on health and :Destroy() the Object Value that is inside the player, you could also set it’s value to nil.

If you have other ways of dealing with this problem please comment or dm so we can discuss it!
If you have any questions or suggestions please you can also comment!

Thank you guys, I hope this was helpful and don’t forget to have fun ! :slight_smile:

10 Likes

Other ressources : How to make a kill counter

2 Likes

Thank you! I’ve been trying to figure out how to do this with the classic sword for a while now.

1 Like

Wow. I didn’t know somebody would I actually show my solution as a resource. Thanks! I believe this would help a lot of people!

1 Like

Yeah, well I basically did this tutorial for someone i was trying to help but after i completed it, I checked on the devforum if someone had done a tutorial or an awnser like mine and I found yours lol.

1 Like