Issue with Kill NumberValue Script

Goal: NumberValue is increased when the player kills someone

Script:

game.Players.PlayerAdded:Connect(function(player)

	local kills = Instance.new("NumberValue")
	kills.Name = "Kills"
	kills.Value = 0
	kills.Parent = player

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value 
			if tag and killer then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)

You would want to use Humanoid.Died to detect the player dying then you would check for the creator value finding the killer. An issue I see is that the kills value is not in leaderstats and also you can add values like this:

killer.leaderstats.Kills.Value += 1

An example code would be

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local killValue = Instance.new("NumberValue")
	killValue.Parent = leaderstats
	killValue.Name = "Kills"

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if humanoid then
			humanoid.Died:Connect(function()
				local tag = humanoid:FindFirstChild("creator")
				if tag then
					local killer = tag.Value
					if killer then
						killer.leaderstats.Kills.Value += 1
					end
				end
			end)
		end
	end)
end)
1 Like

Thank you, I really appreciate this!

Sadly, when I tested this in studio it did not add a value. Does this only work when in-game against an actual player, or if I use a test dummy in studio it will also work? :smiley:

This only works with another user

Is there any errors or anything, are you adding the tags when you damage someone?

change part this to

local killer = players:FindFirstChild(tag.Value)
if tag and killer then
	killer.leaderstats.Kills.Value += 1
end