Kill death leaderstats script isn't working

The title explains it all. Here is my code:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local kills = Instance.new("NumberValue", leaderstats)
	kills.Name = "Kills"
	
	local deaths = Instance.new("NumberValue", leaderstats)
	deaths.Name = "Deaths"
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:FindFirstChild("Humanoid")
		
		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local tag = Humanoid:FindFirsChild("creator")
			local killer = tag.Value
			if tag and killer then
				killer.leaderstats:FindFirstChild("kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
			end
		end)
	end)
end)

The script is a server script inside of ServerScriptService.

1 Like

im not experienced in scripting but i know little
so i think it wont work because you havent done the same thing for kills like add deathvalue + 1

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

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

	local deaths = Instance.new("NumberValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats

	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:FindFirstChild("Humanoid")

		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			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)
end)

When creating an instance through a call to the function “Instance.new()” you should parent the created instance after the instance has been created. This wasn’t the main issue however, the main issue occurs here.

local tag = Humanoid:FindFirsChild("creator")

FindFirsChild should be FindFirstChild.

1 Like

I made Kills = Kills + 1 right here. ^

1 Like

Did my changes fix the script?

An easier way to add, subtract multiply divide, etc is to do += this saves a lot of time and doesn’t require you to type the same thing and add a number

No, but thank you for catching that typo.

Try and show any console errors which occur when you wipeout/knockout.

Problem is this line.
What is the ClassName for the creator tag? Is it a StringValue? Or something? Because if it is a string, then you need to retrieve their player instance through Players[tag.Value].

killer.leaderstats:FindFirstChild("kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1

You forgot to make the K uppercase as you named it “Kills” and not “kills”

You’re making a mistake. The killer variable is the value of the tag inside the Humanoid. What you need to do is replace the local killer = tag.Value with local killer = game.Players:FindFirstChild(tag.Value)

He could be using an ObjectValue to contain the killer.

1 Like

Then in that case, I think the only problem was that he misspelled FindFirstChild

He also needs to make both “k” uppercase in this part:

killer.leaderstats:FindFirstChild("kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1

As he named the value “Kills”

kills.Name = "Kills"

The reason why this doesn’t work is because it only detects if the player dies the moment the player joins. This means that the script will activate once the player joins and since the player hasn’t died yet, the script will not activate and the code will finish running.

A solution to this would be to delete everything in the event function after the leaderstats stuff and put a script in playerscripts or characterscripts instead.

Not how Roblox’s events work. They call every time that the event happened so if the player died at any time the function will call. Also it register the event each time the character respawns.

Not true. They run every time the player dies. Same as what @IA_Restart said. They call every time that the event happened so if the player died at any time the function will call.

You and @AridFights1 are correct I’m sorry

1 Like

It’s fine, we all sometimes learn something wrong! We later learn the right thing!

I have fixed everything you all have helped me with and it still doesn’t work. This error comes up:
ServerScriptService.leaderstats:17: attempt to index nil with 'Value'
Here is my new script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local kills = Instance.new("NumberValue", leaderstats)
	kills.Name = "Kills"
	
	local deaths = Instance.new("NumberValue", leaderstats)
	deaths.Name = "Deaths"
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:FindFirstChild("Humanoid")
		
		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local tag = Humanoid:FindFirstChild("creator")
			local killer = game.Players:FindFirstChild(tag.Value)
			if tag and killer then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)
end)

What could be going wrong?