I'm making a game, there are rounds, and if there is only one person left in the global table, then the game will make the round end and give them points.
There is a problem, where I have it set up so it will teleport everybody into a match, instead of just the people inside of the table, which means if somebody joins the game after the players have been inserted into the table, but before they have been teleported, they get teleported into the game too.
I have tried to solve this problem, but it didn't work. There are no errors in the output.
Here is the code with the problem, but it's shortened down.
_G.gameplayers = {}
for i,v in pairs(game.Players:GetPlayers()) do -- Iterates through a table of online players
if v ~= nil then
table.insert(_G.gameplayers, v.Name) -- Puts them in the table
end
end
local spawns = chosenMap:WaitForChild('Spawns'):GetChildren()
for _, player in pairs(game.Players:GetPlayers()) do
if _G.gameplayers[player] and player and #spawns > 0 then
end
end
The part that I tried to get the players in the table is on line 10, _G.gameplayers[player]
Any help is appreciated, and if more of my code is required, let me know. Thanks.

7 comments
Thank you for what you said about my question. :). I'll now test your solution and let you know if it works or not. Thank you.
I have used your second solution, but I am now getting an error with this
local torso = player.Character:WaitForChild('Torso')the error is 'attempt to index field 'Character' (a nil value)'You will need to check that the Character of the player exists before doing anything with it.
+1, but: 1. There's no reason to use "pairs" instead of "ipairs" when you have a simple list. 2. "if v ~= nil" is useless; if the value is nil, neither pairs/ipairs will give you that value. 3. The "if #spawns > 0" check should ideally be performed once before you iterate over all the players (rather than checking it for each player).
@mosski123, you are getting that error because kools made a mistake -- near the end of line 4 it should say "v", not "v.Name"
I missed those mistakes (from OP's code). I'll update my answer for future reference however.
ipairs was my mistake however I think.