Unable to insert someone into a table

I’ve been trying to get this script to work for a while, but it just doesn’t work. I’m trying to make a zombie game in which when a player dies, they are inserted into an aliveplrs table, and when eliminated are put into a deadplrs table. However, for some reason, the player isn’t being inserted into the table when I want him to.

When I have a respawn wave where all players that were killed are respawned, the player comes back. However, he is not inserted into the deadplrs table, making it so if anyone who was alive before dies, the game ends regardless of how many people there are still left in the game

Here is the code for inserting people into tables. I can send the entire main script as well if you would like.

	for i, plr in pairs(game.Players:GetPlayers()) do

	--	plr:LoadCharacter()

		table.insert(alivePlrs, plr)

		plr.CameraMaxZoomDistance = 0.5
		plr.CameraMinZoomDistance = 0.5
		local RemoteCamEvent = replicatedstorage:WaitForChild("ChangeCameraCustom")
		RemoteCamEvent:FireClient(plr)
		plr.PlayerGui.Menu.Enabled = false

		plr.Character.Humanoid.Died:Connect(function()
			table.remove(alivePlrs, alivePlrs[plr])
			table.insert(deadplrs, plr)
			plr.CameraMaxZoomDistance = 50
			print("Player has been added to the dead players table")
			--plr.PlayerGui.Menu.Enabled = true
		end)

		--gun:Clone().Parent = plr.Backpack
	end
	

And here is the code for respawning players

					for i, player in pairs(deadplrs) do
						if player then
							character = player.Character

							if character then
								-- teleport them
								local RemoteCamEvent = replicatedstorage:WaitForChild("ChangeCameraCustom")
								RemoteCamEvent:FireClient(player)
							wait(1)
							player.CameraMaxZoomDistance = 0.5
								player.CameraMinZoomDistance = 0.5
								player.PlayerGui.Menu.Enabled = false
								table.insert(alivePlrs, deadplrs[player])
								print("Added to alive players table")
								character:FindFirstChild("HumanoidRootPart").CFrame = availablespawnpoints[1].CFrame + Vector3.new(0, 10, 0)
							--	player.PlayerGui.Menu.Enabled = false

								table.remove(availablespawnpoints, 1)

								--give sword

								local equipped = game.ServerStorage.PlayerData[player.Name].Equipped

								if equipped.Value ~= "" then
									local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
									weapon.Parent = player.Backpack
								else
									local gun = serverstorage.M9:Clone()
									gun.Parent = player.Backpack
								end

							else
								-- no character found
								if not player then
									table.remove(alivePlrs, i)
								end
							end
						end
					end

The problem is in the respawn script when trying to insert the player back into the aliveplrs table. “table.insert(alivePlrs, deadPlrs[player])”. It doesn’t insert the player back into the table.

Could you try debugging it? What’s the output like?

I’m not getting any errors in the output related to the table issue.

Does it print anything from your debug?

Does it print("Player has been added to the dead players table")?

It moves past the line with no errors.
It does print the line

Try this: table.insert(alivePlrs, player), your already searching through the dead players table so you shouldn’t need to try and get the from the table again with the key. Also, you may wan’t to remove the player from the deadplrs table (after you insert them into the alive players).

I added table.remove(deadPlrs, player) after inserting the player into the aliveplrs table, and I got an error saying " Invalid argument number 2 number expected got instance". Also when I do table.insert(alivePlrs, player), it breaks the entire script, and causes it so if all players die, the round still continues on and the players are stuck at the menu.

try table.remove(deadPlrs, i) or something using the index. I forgot that table.remove will use an index if there is no key to call it by.

It breaks out of the entire game system. If all players die, the round still continues.

table.insert() has 3 arguments, the first being the table you’re putting it in, the second being the order it’s placed in, and the third is the actual thing being placed in the table

table.insert(alivePlrs, 1, deadplrs[player])

Perhaps this would fix it

I tried that. Once the dead players respawn, it automatically says that all players were eliminated and resets the game.