Help with player becoming nil when they leave game

So I created a game loop, and at the start of the loop, I got all the player objects and put them inside an array. When doing stuff to the player, like teleporting, assigning they’re team, when they leave the game, the player object in the array corresponding to the player leaving turns into nil. However, I have a simple if check making sure that when I try to assign a team to a player who is now nil as they have left the game, it removes them from the table, like this:


However, I get this stupid error:

Edit: This error also happens to other parts of my script where it check if its nil, but it doesn’t pick up for some reason, and it still tries to edit the player object even tho it is nil and doesn’t remove it, even tho the if statement clearly checks if the player object is nil.

Instead of checking for plr == nil, try checking plr.Parent == nil.

1 Like

Avoid iterating while using table.remove. There is an entire stack exchange question based on this issue:

1 Like


There are so many if statements that I can’t tell what’s going on. I’m assuming that this is a server script, so when a player leaves, Players.PlayerRemoving fires. You can use the player it returns to remove the player from your table. You can also use the Players.PlayerAdded event instead of using a constant loop.

There’s also a Team.PlayerRemoved event that you could use to trim down your code further. As it is, I can’t understand what’s going on.