How to make a kill counter

  1. What do you want to achieve? Keep it simple and clear!
    How can i make a leaderstats that counts how many kills you have.

  2. What is the issue? Include screenshots / videos if possible!
    I have tried lots of youtube tutorials for this but none of them work.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Youtube and Devforum

4 Likes

Leaderstats Script with Kills

Here’s an example of a leaderstats script with kills that you can use as a starting point:

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

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Value = 0
    kills.Parent = leaderstats
end)

This script creates a new Folder object named leaderstats for each player that joins the game. Inside this folder, an IntValue object named Kills is created and set to 0. This value will be used to track the number of kills for each player.

You can also find more examples and tutorials on how to create a leaderstats script with kills on websites like Roblox Creator Documentation or on YouTube.

3 Likes

Yea, i already know how to make leaderstats, but i need to make it so that when a player dies it somehow gets the players name that killed them

1 Like

One way to do this is by using a Humanoid object and connecting a function to its Died event. Inside this function, you can use the FindFirstChild method on the Humanoid object to find a child object with the name "creator" . This child object should contain information about the player that killed them.

Here’s an example script that demonstrates this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:connect(function(character)
        local hum = character.Humanoid
        hum.Died:Connect(function()
            local tag = hum:FindFirstChild("creator")
            if tag ~= nil then
                -- tag.Value contains information about the player that killed them
                local killerName = tag.Value.Name
                -- do something with killerName here
            end
        end)
    end)
end)

This script listens for when players are added to the game and when their characters are added. When a character’s Humanoid dies, it checks for a child object with the name "creator" and retrieves its value which should contain information about the player that killed them.

2 Likes

Ok thanks so much, ill try this asap

2 Likes

btw, is this is a script or a local script and where would i put it

Server script inside ServerScriptServices

It is a server script, you can add it to the leaderstats script.

its not working, rn im making it print the killers name and it doesnt work

Hmm. Try using FindFirstChild for the player’s humanoid.

1 Like

Also, could you try killing a player with the linked sword?

1 Like

but also i get no errors, it just doesnt print the killers name

i have to go now, cya ill try this some other time when i can

For example, you can do this for a tool that you can kill another player as linked sword does:

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

image
Try taking a look in the server script of the linked sword.

It would be beneficial to attempt to debug this on your own.

Try using print statements around certain lines of code to see if it runs said code, like this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:connect(function(character)
        local hum = character.Humanoid
        hum.Died:Connect(function()
            print("player died") -- checking to see if the .Died event has run
            local tag = hum:FindFirstChild("creator")
            if tag ~= nil then
                print("tag is not nil, someone had killed the player") -- checking to see if this if statement has run
                local killerName = tag.Value.Name
                print(killerName) -- checking to see if there is a value for the killername
            end
        end)
    end)
end)

Printing is a very powerful tool in programming.

Please note, however, that I am not against you asking for help, just for someone who makes tutorials on programming, you should know necessary steps to debug code.

1 Like

I already tried that, still nothing

I dont want to make a script that i have to put inside of all my tools because i have too many

Updated leaderstats:

Players = game:GetService(“Players”)

local function leaderstatsData(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

Local cash = Instance.new(“IntValue”)
cash.Name = “Cash”
cash.Value = 100
cash.Parent = leaderstats
end)

game.Players:PlayersAdded:Connect(leaderstatsData)

This should be a updated version of your current leaderstats for anyone who may want it.

I wasn’t offering a solution, merely giving you options to debug the code.

The print statements won’t make your code work, they will only tell you what part of the code has been reached and executed.

If any of the print statements have been outputted, then it means the previous line of code has been executed, meaning it works, else, there is something wrong with it,

:slight_smile:

To create a leaderstats that counts how many kills a player has, you can use a combination of a ServerScriptService script and a LocalScript in a player’s PlayerGui.

Here are the steps to follow:

  1. Create a new ServerScriptService script and name it “KillsTracker” or something similar.
  2. Inside the script, create a function to update a player’s kill count when they kill an enemy. This function should be called whenever a player kills an enemy. You can use the “leaderstats” service to update the player’s kill count.

Here’s an example function that updates a player’s kill count:

function onEnemyKilled(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local kills = leaderstats:FindFirstChild("Kills")
        if kills then
            kills.Value = kills.Value + 1
        end
    end
end
  1. Create a new LocalScript inside a player’s PlayerGui and name it “KillsDisplay” or something similar.
  2. Inside the LocalScript, create a reference to the player’s leaderstats and create a new IntValue instance for the player’s kill count. You can use a “Folder” instance to group the IntValue with other leaderstats.

Here’s an example LocalScript:

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")

local killsFolder = Instance.new("Folder")
killsFolder.Name = "KillsFolder"
killsFolder.Parent = leaderstats

local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = 0
kills.Parent = killsFolder
  1. To display the kill count to the player, you can use a TextLabel in the player’s PlayerGui. You can set the Text property of the TextLabel to the player’s kill count using a “BindableFunction” or by using a “Heartbeat” event in the LocalScript.

Here’s an example TextLabel that displays the player’s kill count:

local killsTextLabel = script.Parent.KillsTextLabel

function updateKills()
    local kills = leaderstats.Kills.Value
    killsTextLabel.Text = "Kills: " .. tostring(kills)
end

updateKills()

game:GetService("RunService").Heartbeat:Connect(updateKills)

This TextLabel should be a child of the LocalScript’s parent object, and it should have a “Text” property that displays the player’s kill count.