I have a minigame system that detects when a player dies. The issue here is that it sometimes breaks. I have yet to find a solution to this.Here is the code
for i, player in pairs(game.Players:GetPlayers()) do
if player.CharacterAppearanceLoaded and player.Character then
table.insert(playersInRound, player)
table.insert(playerInRoundNames, player.Name)
end
player.Character:WaitForChild("Humanoid").Died:Connect(function()
local ind = table.find(playersInRound, player)
if ind then
table.remove(playersInRound, table.find(playersInRound, player))
table.remove(playerInRoundNames, table.find(playerInRoundNames, player.Name))
end
for _, player in pairs(game.Players:GetPlayers()) do
player.PlayerGui.Gamemode.PlayersLeft.PlayersLeftText.Text = #playersInRound
end
end)
end
the error is "attempt to index nil, with ‘WaitForChild’
that is because the loop is still running and if the player dies while the loop is looping another player and then loops that player then it will throw an error
and the added players script
its a really simple way to implement but I think you could do something better
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
-- creates a value for IsAlive
local IsAlive = Instance.new("BoolValue")
IsAlive.Name = "IsAlive"
IsAlive.Parent = Player
Character.Humanoid.Died:Connect(function()
IsAlive:Destroy() -- other words false
end)
end)
end)