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

Killing for Kills returns with "attempt to index local 'player' (a nil value)"?

Asked by 5 years ago

I have an IntValue inside the player that is a kill counter that adds one number for every kill someone does. When i try killing another player, it returns with the error: "attempt to index local 'player' (a nil value)". How can i fix this?

game.Players.PlayerAdded:connect(function(plr)  
plr.CharacterAdded:connect(function(char)
    char.Humanoid.Died:connect(function()

    if char.Humanoid:FindFirstChild("creator") ~= nil then
        local tag = Instance.new("StringValue")
        tag.Value = char.Humanoid.creator.Name
        local player = game.Players:FindFirstChild(tag.Value)
        player.KillCounter.Value = player.KillCounter.Value + 1
        tag:Destroy()
    end
end)
end)
end)
0
Because player wasn't found. User#19524 175 — 5y
0
so how can i change it to find the player? BrawlBattle 15 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

This is happening because player wasn't found. You will need an if statement to check if it was found.

local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(plr)  
    plr.CharacterAdded:Connect(function(char)
        char.Humanoid.Died:Connect(function()

            if char.Humanoid:FindFirstChild("creator") then -- no need for ~= nil
                local tag = Instance.new("StringValue")
                tag.Value = char.Humanoid.creator.Name
                local player = Players:FindFirstChild(tag.Value)

                if player then -- here check if the player exists
                    player.KillCounter.Value = player.KillCounter.Value + 1
                    tag:Destroy()
                end
            end
        end)
    end)
end)

0
It doesn't seem to be working when i try killing someone, but i did find something else in the console that i don't know if it has something to do with it. It said "Request was Throttled. Try sending fewer requests. Key = syemoom". BrawlBattle 15 — 5y
0
the damage script is inflicting too much damage making the character die too much. Mr_Unlucky 1085 — 5y
Ad

Answer this question