Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to track a player's KO's in a game?

Asked by 2 years ago

Hi, I am creating a game where the game will need to save and update the KO's. But, to accomplish that, the game needs to track KO's. How would I even BEGIN to do this?!

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Here, maybe this could help get you started:

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)
        Deaths.Value = Deaths.Value + 1
        local Humanoid = Character:FindFirstChild "Humanoid"
        if Humanoid then
            Humanoid.Died:connect(function()
                for i, Child in pairs(Humanoid:GetChildren()) do
                    if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                        local Killer = Child.Value
                        if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                            local Kills = Killer.leaderstats.Kills
                            Kills.Value = Kills.Value + 1
                        end
                        return
                    end
                end
            end)
        end
    end)
end)

So pretty much in this script, its a leaderboard for the players kills and deaths. Now, if you were wanting to just have the KO's and not have the deaths, it would look something like this:

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "KO's" --- Name of what you want your scoreboard to say EX: Kills, KO's

Players.PlayerAdded:connect(function(Player)
    wait(1)
    local Stats = Template:Clone()
    Stats.Parent = Player
    Player.CharacterAdded:connect(function(Character)
        local Humanoid = Character:FindFirstChild "Humanoid" ---- Naming the humanoid in the game.
        if Humanoid then
            Humanoid.Died:connect(function() --- On the players death, these lines of code run.
                for i, Child in pairs(Humanoid:GetChildren()) do
                    if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
                        local Killer = Child.Value
                        if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
                            local Kills = Killer.leaderstats.Kills
                            Kills.Value = Kills.Value + 1 ---- Adds 1 to the Kill value or in your case, the KO value.
                        end
                        return
                    end
                end
            end)
        end
    end)
end)

In this script, I removed the new instance stated deaths, and all of the death coding in the script. I also changed the name of Kills to KO's for you. Hopefully this helps you at all.

0
Thanks! Finally, I can continue with my game :D mine_theblocks 23 — 2y
Ad

Answer this question