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

Leaderstats kills not working?

Asked by 1 year ago

Can someone please explain to me why the kills are not working on this leaderstats script? Thanks!

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)
0
what's not working? the value doesn't get updated? or the value isn't saved? WINDOWS10XPRO 438 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

I see. Player.CharacterAdded won't fire when a player joins or the Players.PlayerAdded event. To fix this, you create a Script inside StarterCharacterScripts.

Why put in StarterCharacterScripts? It's because everything inside StarterCharacterScripts will be moved to the player's character once it loaded/respawned. Not only it will be moved, but making a Script a descendant of Workspace (Player.Character is a descendant of Workspace too) will automatically start running the script.

After creating the Script, copy this code:

local Players = game:GetService("Players")

local Character = script.Parent
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Player = Players:GetPlayerFromCharacter(Character)

local Stats: Folder = Player:FindFirstChild("leaderstats")
local Kills: IntValue = Stats:FindFirstChild("Kills")
local Deaths: IntValue = Stats:FindFirstChild("Deaths")

Humanoid.Died:connect(function()
    for _, Child in pairs(Humanoid:GetChildren()) do
        local Killer = Child.Value

        if Child:IsA("ObjectValue") and Killer and Killer:IsA("Player") then
            local OtherStats = Killer:FindFirstChild("leaderstats")
            local OtherKills = OtherStats:FindFirstChild("Kills")

            if OtherStats and OtherStats:IsA("Folder") and OtherKills and OtherKills:IsA("IntValue") then
                Deaths.Value += 1
                OtherKills.Value += 1
            end
        end
    end
end)

Make another Script inside ServerScriptService and this is where we make the leaderstats.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    local Stats = Instance.new("Folder", Player)
    Stats.Name = "leaderstats"

    local Kills = Instance.new("IntValue", Stats)
    Kills.Name = "Kills"

    local Deaths = Instance.new("IntValue", Stats)
    Deaths.Name = "Deaths"
end)

After doing all that, you may now delete your old leaderstats script.

0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
View all comments (10 more)
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
did not work nothing happens when a player is killed kickoff127 103 — 1y
0
how? T3_MasterGamer 2189 — 1y
0
did you follow the instructions? T3_MasterGamer 2189 — 1y
0
also you didn't have to spam it's rude and breaking the rules T3_MasterGamer 2189 — 1y
Ad

Answer this question