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

Why does my kills leaderstats not work?

Asked by 2 years ago

My leaderstats only works for the deaths, but only for me. The kills do not work at all. The captures work as well.

local leaderstats = Instance.new('Model', player)
    leaderstats.Name = 'leaderstats'

    local Captures = Instance.new('IntValue', leaderstats)
    Captures.Name = 'Captures'
    Captures.Value = 0

    local Kills = Instance.new('IntValue', leaderstats)
    Kills.Name = 'Kills'
    Kills.Value = 0

    local Deaths = Instance.new('IntValue', leaderstats)
    Deaths.Name = 'Deaths'
    Deaths.Value = 0

    local Players = game.Players



    Players.PlayerAdded:Connect(function(player)
        wait(1)
        local Stats = leaderstats: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)

Can you tell me why the kills are not working? Thanks!

2 answers

Log in to vote
1
Answered by 2 years ago

Maybe you could attempt doing a script to check for humanoid health. I don't know how to script it but it would need a loop. It would also not do a favour for performance but it might work

Ad
Log in to vote
1
Answered by 2 years ago

Top of your script is a bit confusing, but I assume that you have more above it.

First thing I notice is the wait(1) after the player joins. This makes it so the first time they spawn in, you don't detect their character, suggest that be removed (could also be a reason why kills don't work if you only test/kill the other player one time). Also when you add deaths, you should do it when they die instead of when they spawn in.

I also see that you use quite a bit of unnecessary if statements, like checking for the humanoid (if theres no humanoid, theres obviously something wrong), but if you like keeping them there, you should probably print something after to make sure its passing the point in the script.

example of how you could change your script (sorry if it looks weird, only intended to send the characteradded function at first)

Players.PlayerAdded:Connect(function(player)
local Stats = leaderstats:Clone()
Stats.Parent = player
local Deaths = Stats.Deaths

player.CharacterAdded:Connect(function(Character)
    local Humanoid = Character:WaitForChild("Humanoid")
    Humanoid.Died:Connect(function()
        Deaths.Value += 1 -- deaths should be added 
        local creator = Humanoid:FindFirstChild'creator' -- how roblox swords marks their killer, can use Humanoid:FindFirstChildOfClass'ObjectValue' if its different, assuming this based on how your script was set up. using this rather than a loop is a lot more efficient 
        if creator then
            local killer = creator.Value
            local ls = killer:WaitForChild('leaderstats')
            ls.Kills.Value += 1
        end
    end)
end)

end)
0
should I put this instead of the kills and deaths? kickoff127 103 — 2y

Answer this question