Table printing empty when there are contents inside

Hey there, I’m trying to make a round system for one of my games that I’m working on, and for some reason, it’s not working.

Error: So, you may notice in the code I put several print statements to print out the playersInRound table. However, what’s happening that is weird, is that at first it prints out the entire table perfectly fine over and over.

But, right when the second-to-last player dies, where it’s supposed to end the game and announce the winner, that last print statement outside of the while loop prints that the table is empty.


As you see here, it prints the table fine, then right as the second to last player dies, it prints an empty table.

local status = game.ReplicatedStorage.Status
local replStorage = game.ReplicatedStorage


function startGame()
	local playersInGame = game.Players:GetPlayers()

	for i, player in pairs(game.Players:GetPlayers()) do
		local char = player.Character
		if char then
			local humanoidRootPart = char.HumanoidRootPart
			humanoidRootPart.CFrame = game.Workspace:FindFirstChild("TpToPart").CFrame
		end
	end
	
	
	while #playersInGame > 1 do
		task.wait()
		print(playersInGame)
		for i, v in pairs(playersInGame) do
			v.Character.Humanoid.Died:Connect(function()
				local index = table.find(playersInGame, v)
				table.remove(playersInGame, index)
			end)
		end
		
		print(playersInGame)
		
		game.Players.PlayerRemoving:Connect(function(plr)
			local index = table.find(playersInGame, plr)
			if not index then 
				
			else
				table.remove(playersInGame, index)
			end
		end)

		status.Value = "Game in progress..."
		
		
		print(playersInGame)
		
		replStorage.DestroyPart.OnServerEvent:Connect(function(player, item)
			item:Destroy()
		end)

		print(playersInGame)
	end


	print(playersInGame)
	
	local winner = playersInGame[1]
	
	status.Value = "Winner: "..winner.Name
	winner.leaderstats.Wins.Value += 1
	winner:LoadCharacter()
	game.Workspace:FindFirstChild("Map"):Destroy()
	local clone = replStorage.Map:Clone()
	clone.Parent = workspace
	task.wait(5)

end

while true do
	for i = 10, 0, -1 do
		status.Value = "Game starting in: "..i
		task.wait(1)
	end
	startGame()
end
1 Like

The Humanoid.Died function can sometimes remove people twice causing this. So I suggest doing something like this which is more accurate (place at top of script outside of while loop)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local index = table.find(playersInGame, player)
			if not index then 

			else
				table.remove(playersInGame, index)
			end


you could always just use Once which is a Method on RBXScriptSignals which returns a RBXScriptConnection which also automatically disconnects itself whenever it is fired once.

character:WaitForChild("Humanoid").Died:Once(function()
      --code
end)
2 Likes

Wow that’s cool, I did not know that!

Interesting, I’ll try that, thanks!

I did manage to figure out how to do it a different way, with teams, but I’ll mark yours as a solution if anyone else has a similar issue!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.