Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

Because player wasn't found.

0

so how can i change it to find the player?

1 answer

###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)

2 comments

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".

0

the damage script is inflicting too much damage making the character die too much.

This discussion is preserved as it was. New answers, comments, and votes are closed.