Leaderboard first 2 kills to get 1 kill

Hello!

I’m having an issue with my leaderboard system in my Roblox game. The leaderboard correctly displays kills and deaths, but there’s a strange bug when it comes to counting kills. When I kill a player for the first time, the kill count remains at 0. It only updates to 1 after I kill a second player. However, the death count works perfectly and updates immediately.

Also there are nothing errors in the console.

I’ve shared my code below for reference. I would appreciate any help or insights into why the kills are not being counted correctly on the first kill. Thank you for help!

to the Video

local Players = game.Players

local Template = Instance.new('BoolValue')
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"


Players.PlayerAdded:connect(function(Player)
	wait(1)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
	Player.CharacterAdded:connect(function(Character)
		Deaths.Value = Deaths.Value + 1
		local Humanoid = Character:FindFirstChild ("Humanoid")
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return 
					end
				end
			end)
		end
	end)
end)

the Deaths.Value = Deaths.Value + 1 line is inside the Player.CharacterAdded event, which means it adds every time a character is added, not when a player dies.

local Players = game.Players

local Template = Instance.new('BoolValue')
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"

Players.PlayerAdded:Connect(function(Player)
	local Stats = Template:Clone()
	Stats.Parent = Player

	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:Connect(function()
				local killer = Humanoid:FindFirstChild("creator")
				if killer and killer.Value and killer.Value:IsA("Player") then
					local KillStats = killer.Value:FindFirstChild("leaderstats")
					if KillStats then
						local KillerKills = KillStats:FindFirstChild("Kills")
						if KillerKills then
							KillerKills.Value = KillerKills.Value + 1
						end
					end
				end
				local PlrStats = Player:FindFirstChild("leaderstats")
				if PlrStats then
					local PlrDeaths = PlayerStats:FindFirstChild("Deaths")
					if PlrDeaths then
						PlrDeaths.Value = PlrDeaths.Value + 1
					end
				end
			end)
		end
	end)
end)

Im gonna try and figure out what the problem is, but dont add one death to the player when they get a new character, do it when they well, die, otherwise as of now you add a death to them even when they were to just join the game, which is a little annoying.

As for giving players kills, Id advise you add some print statements, for instance print if they do include the objectValue that is assigned to the player, and print if the player does have leaderstats, and print to just make sure the code where you add kills is actually running.

Also sometimes the character might already be loaded in for whatever reason so do this instead:

local function handleCharacter(Character:model)
        local Humanoid = Character:FindFirstChild ("Humanoid")
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return 
					end
				end
			end)
		end
end

players.PlayerAdded:connect(function(Player)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
    if player.Character then handleCharacter(player.Character) end
    player.CharacterAdded:connect(handleCharacter)
end

Ok from another look the reason why this prob isnt working is due to you having the wait(1) in the code, this causes the code to wait, and by then the character is probably loaded in, therefore the player.CharacterAdded event is never fired, as the character already exists, so this code here should fix it.

Basically what I do is just check if the character is already loaded, if so, trigger a function which does all that stuff to the character, I then also set the player.CharacterAdded event to trigger that function.