How Can I Make NPC Kills Count

Hi I’m The_BorwnDoggy, this is my first post on this forum. I needed some help with making my kills count on my leaderstats. The reason its not working is because I’m making an FPS game, (my first game), and the way the NPCs are kill is with a gun, which is a tool.

Here the code I have for the leaderstats which as a said doesn’t work.

-- 
    local function addBoard(player)
	local board = Instance.new("Folder", player)
	board.Name = "leaderstats"
	
	local kills = Instance.new("IntValue", board)
	kills.Name = "Kills"
	
	local deaths = Instance.new("IntValue", board)
	deaths.Name = "Deaths"
end

local function onCharacterRemoving(char)
	local player = game.Players:GetPlayerFromCharacter(char)
	local deaths = player.leaderstats.Deaths
	deaths.Value = deaths.Value + 1
end

local function onPlayerAdded(player)
	addBoard(player)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

I would be extremely happy if anyone could help me with this :slight_smile:

1 Like

Track the NPC’s death using Humanoid.Died event.

Also, Kills are different from Deaths. Remember that necessarily someone that dies isn’t killed, I recommend you look for who killed who when someone uses a weapon, aka your gun tool(s).

1 Like

Thanks for the quick response,

Would I need to make a new local script inside the tool to track the NPC’s death, or just add it to my leaderstats one

To accurately track deaths/kills, it should not be updated from a local script nor should any killing be recorded through the local script. This is open to exploitation.

Local scripts can detect the action of the player pressing their mouse and thus allowing you to have them shoot their gun but the bullet hitting whom should be checked or done on the server before allowing any damage and then you can track for kills that way.