Table won't work?

Hello, I am making a last man standing game and I have a table for the players ingame. But for some reason the players wont add. I have a few other bugs this leads to aswell so this is a big issue.
Here is my code:

wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

	local function restart()
		script.Disabled = true
		wait()
		script.Disabled = false
	end
local maps = {
	ReplicatedStorage.ForestMap,
	ReplicatedStorage.WorshipMap, 
	ReplicatedStorage.NeonMap
}
local gameStatus = ReplicatedStorage.gameStatus

while true do
--Waiting
	gameStatus.Value = "Waiting for Players"
	
--repeat wait() until Players.NumPlayers > 1
--Intermission
	gameStatus.Value = "Intermission, Time Left: "
	local timeLeft = 15
	for i = 1, timeLeft do
		gameStatus.Value = ("Intermission, Time Left: " .. timeLeft)
		wait(1)
		timeLeft -= 1
	end
	
--StartingGame
	local chosenMap = maps[math.random(1, #maps)]
	
	chosenMap.Parent = game.Workspace
	
	local matchPlayers = {}
	
	for i, v in pairs(Players:GetPlayers()) do
		local Position = table.find(matchPlayers, v)
		table.insert(matchPlayers, Position)
	end
	
	print(#matchPlayers)
	gameStatus.Value = "Game In Progress"
	
	for i, v in pairs(matchPlayers) do
		v.Character:MoveTo(Vector3.new(math.random(-30, 30), 30, math.random(-30, 30)))
		local sword = ReplicatedStorage["Wooden Sword"]:Clone()
		sword.Parent = v.Backpack
	end
	
--Game
	while true do
		wait()
		for i, v in pairs(matchPlayers) do
			
			v.Character.Humanoid.Died:Connect(function()
				local position = table.find(matchPlayers, v)
				table.remove(matchPlayers, v)
			end)
			
			Players.PlayerRemoving:Connect(function(v)
				local position = table.find(matchPlayers, v)
				table.remove(matchPlayers, v)
			end)
			
		end
	end

	--WinnerRewardAndGameEnd
	if #matchPlayers == 1 then
		print("game ended")
		print(matchPlayers[1])
		matchPlayers[1].leaderstats.Wins.Value += 1
		print(matchPlayers[1].leaderstats.Wins.Value)
		matchPlayers[1].Character.Humanoid.Health = 0

		chosenMap.Parent = ReplicatedStorage
		
		restart()
	end
end

The restart thing is pointless, it stops the script and then the script won’t run anything else so it won’t reenable it, also use task.wait() as wait() is deprecated.

Try calling a bindableevent and attach another script to it and make that script restart it.

I tried replacing the waits with task.wait, and I put the script in a while true do so it will go on forever. But the table still doesn’t work.

wouldn’t changing

local matchPlayers = {}
	
	for i, v in pairs(Players:GetPlayers()) do
		local Position = table.find(matchPlayers, v)
		table.insert(matchPlayers, Position)
	end

to
local matchPlayers = Players:GetChildren()
fix the issue?

The problem lies here, you’re trying to find the “Position” of a player in a table, where it has never been inserted, which would return nil, after that, you insert its “Position” into the table, but since it is nil, it won’t insert anything, leaving the table empty.

Simply rewrite your code to this and it should work. table.insert will automatically handle positioning of new entries into it, you don’t need to do it yourself. Cheers!

    for i, v in pairs(Players:GetPlayers()) do
		table.insert(matchPlayers, v)
	end

Well that wouldn’t work very well since I need the Position to get the player. Since it’s an instance.

I did this before but it wouldn’t let me remove an instance item from a table.

this is why it will not work.

character limit

If you want to remove a player from the list later on, what you can do is:

table.remove( matchPlayers, table.find( matchPlayers, player ) )

Although it doesn’t look fancy, this is a work around for utilizing tables this way. I suggest you try out dictionaries and you might get a better result, however I assure you this will work.

Safe-checking version, if the player you want to remove isn’t on the table in the first place, the code above would just remove the last added player.

local target = table.find(matchPlayers, player)
if target then
   table.remove(matchPlayers, target)
end

Why is this line commented? The script starts before any players join. Uncomment this line and change it to:

repeat task.wait() until #Players:GetPlayers() >= 1

Repeats waiting until the amount of the players is greater than or equal to 1, nothing too complicated.

Use > instead of >= if you want at least 2 players.

It’s commented so I can test. It’s just the intermission.

I’m gonna use a while true do, since I can just break, and I need to remove from the player table when somebody leaves/dies.

Also, I think I should mention that the script doesn’t end when there is one player as it should.