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

How can I fix this Kill Added script? I need help please and thank you

Asked by 2 years ago

Hello! I am working on a FFA game and when I wanted to make a kill counter, it doesn't work and I don't get what's happening.

One thing I did notice is that in the output it said that I attempted to index nil with the "leaderstats" folder.

I'd be happy to find a solution from your answers! Thanks!

Code used:

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
    wait(1)
    local leaderstats = Players.LocalPlayer.leaderstats -- This is the line that the error in the output mentioned
    local kills = leaderstats.Kills
    player.CharacterAdded:Connect(function(character)
        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
                            killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                        end
                    end
                end
            end)
        end
    end)
end)

1 answer

Log in to vote
0
Answered by 2 years ago

Hopefully this works, I checked it out and it looks like you were trying to reference Players.LocalPlayer which only works in LocalScripts, meaning you can't use it in a normal script. Since you already put player in the () on the Players.PlayerAdded you only had to do local leaderstats = player.leaderstats.

Hopefully it works, if it doesn't work make sure you have made it in a normal script. Good luck on your FFA game!

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
    wait(1)
    local leaderstats = player.leaderstats -- This is the line that the error in the output mentioned
    local kills = leaderstats.Kills
    player.CharacterAdded:Connect(function(character)
        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
                            killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                        end
                    end
                end
            end)
        end
    end)
end)
0
It doesn't seem to work, and as long as I changed the "ObjectValue" to "IntValue" at line 12 (because I was using an IntValue for the kill count) still doesn't seem to work and there are no errors printed into output? isir2204 28 — 2y
0
hm. could you maby try to make the script in another way? johnoscarbhv1 137 — 2y
Ad

Answer this question