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