Kills not counting

Alright, so I created script with death/kills leaderstat. When I’m killing player, the kill doesn’t count, but when i’m dying the death is count ( weird ik)

Also, here is my script

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)
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:Connect(function()
				Deaths.Value = Deaths.Value + 1
				print("Player died:", Player.Name)
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Name == "creator" and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						print("Killer found:", Killer.Name)
						if Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
							print("Kills updated for:", Killer.Name)
						end
						return -- Only one player can get a KO for killing a player. Not 2, not 3. Only one.
					end
				end
			end)
		end
	end)
end)

Maybe I should add smthng like owner ( forgot this name)

1 Like

Try to make it easier, put a string value in each character named LastHit, and after each hit update who hit last. And after death, search for the character by nickname and give him 1 point

Otherwise I can’t help you, as you haven’t provided a script for calculating kills. Only the system of point accrual based on Value

Ehh…Doesn’t work… Sadly, I added this

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)
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			-- Create LastHit StringValue in character to store the name of the last hitter
			local LastHit = Instance.new("StringValue")
			LastHit.Name = "LastHit"
			LastHit.Parent = Character

			-- Update LastHit on each hit (assuming there's a system to detect hits)
			Humanoid.HealthChanged:Connect(function(health)
				if health < Humanoid.MaxHealth then
					-- Replace with actual hitter detection logic here
				
					local hitter = detectHitter() -- This should be replaced with the actual function to get the hitter's player name
					if hitter then
						LastHit.Value = hitter.Name
					end
				end
			end)

			-- Detect death and award point to last hitter
			Humanoid.Died:Connect(function()
				Deaths.Value = Deaths.Value + 1
				print("Player died:", Player.Name)

				-- Check LastHit to find the last hitter
				local killerName = LastHit.Value
				if killerName and killerName ~= "" then
					local Killer = Players:FindFirstChild(killerName)
					if Killer and Killer:FindFirstChild('leaderstats') and Killer.leaderstats:FindFirstChild("Kills") then
						local Kills = Killer.leaderstats.Kills
						Kills.Value = Kills.Value + 1
						print("Kills updated for:", Killer.Name)
					end
				end
			end)
		end
	end)
end)

try detecting player with function like there and use userid
also check console to view warns

local Players = game:GetService("Players")
local Character = script.Parent
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local function getPlayerByUserId(userid: number)
	for _, player in pairs(Players:GetPlayers()) do
		if player.UserId == userid then
			print(player.Name)
			return player
		end
	end
	return nil
end

local lastHit = Instance.new("StringValue")
lastHit.Name = "LastHit"
lastHit.Parent = Character

Humanoid.Died:Connect(function()
	if lastHit ~= "" and lastHit ~= nil then
		local getPlayer = getPlayerByUserId(lastHit.Value)
		if getPlayer ~= nil then
			getPlayer.leaderstats.Kills.Value += 1
		else
			warn("not player find")
		end
	else
		warn("last hit = nil")
	end
end)

Kills didn’t count again
Here is screenshot

You have a player located via userId, but no points are being credited. right?

Yes, everything is right. Maybe it’s issue with tools? Cuz I heard that tools can be in that role too

replace this, I forgot that such a function exists. xd
I think it’ll work after that.

local function getPlayerByUserId(userid: number)
	return game.Players:GetPlayerByUserId(userid)
end

If it still doesn’t work, check if lastHit value changes after taking damage. If it doesn’t change the problem is different, because on empty place everything works fine

On baseplate these scripts work fine, I guess if it doesn’t work the same way, the problem is different

script in character:

local Players = game:GetService("Players")
local Character = script.Parent
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local function getPlayerByUserId(userid: number)
	return game.Players:GetPlayerByUserId(userid)
end

local lastHit = Instance.new("StringValue")
lastHit.Name = "LastHit"
lastHit.Parent = Character

Humanoid.Died:Connect(function()
	if lastHit ~= "" and lastHit ~= nil then
		local getPlayer = getPlayerByUserId(lastHit.Value)
		if getPlayer ~= nil then
			getPlayer.leaderstats.Kills.Value += 1
		else
			warn("not player find")
		end
	else
		warn("last hit = nil")
	end
end)

Damage module

local dealDamage = {}

function dealDamage.TakeDamage(character: Model,target: Model,damage: number ,damageType: string)
	
	local TargetHumanoid = target:FindFirstChildOfClass("Humanoid")
	local TargetLastHit = target:FindFirstChild("LastHit")
	
	if not TargetLastHit then return end

	TargetHumanoid:TakeDamage(damage)
	local getPlayer = game.Players:GetPlayerFromCharacter(character)
	if getPlayer then
		TargetLastHit.Value = getPlayer.UserId
	end
	
end

return dealDamage

EXAMPLE SCRIPT::

game.Players.PlayerAdded:Connect(function(addedPlayer)
	
	addedPlayer.CharacterAdded:Connect(function(addedCharacter)
		
		local dealDamage = require(game.ServerScriptService.dealDamage)
		while wait(1) do
			dealDamage.TakeDamage(addedCharacter,workspace.Dummy,21,nil)
		end
		
	end)
	
end)

Still doesn’t work… Idk what the heck is going on…

The following code should fix the problem… let me know if you have any questions about it!

Server Script

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		local tag = Instance.new("ObjectValue") -- used to track who last hit the player (who to give the kill increase, should be handled when handling player hit detection)
		tag.Value = player -- this should be changed to whoever last hit the player but as an example i set is as themself
		tag.Name = "Tag"
		tag.Parent = humanoid
		
		if humanoid then
			humanoid.Died:Connect(function()
				deaths.Value += 1
				
				if humanoid:FindFirstChild("Tag") then
					local killer = humanoid.Tag.Value
					
					killer.leaderstats.Kills.Value += 1
					
					warn(killer, "Killed", player)
				end
			end)
		end
	end)
end)

This is works, but look the video which I pinned here
robloxapp-20241109-1155126.wmv (3.6 MB)