Literally remove an instance from a table?

Hello, I am making a last man standing game, and I need to remove the “players” from a table. The issue is the players are Instances so it won’t let me. Here is the main code:

local matchPlayers = {}
	
	for i, v in pairs(Players:GetPlayers()) do
		table.insert(matchPlayers, v)
	end
while true do
		wait()
		for i, v in pairs(matchPlayers) do
			v.Character.Humanoid.Died:Connect(function()
				table.remove(matchPlayers, v)
			end)
			
			Players.PlayerRemoving:Connect(function(v)
				table.remove(matchPlayers, v)
			end)
			
		end
	end

And here is my full 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
		table.insert(matchPlayers, v)
	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()
				table.remove(matchPlayers, v)
			end)
			
			Players.PlayerRemoving:Connect(function(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

When using the table.insert() function without providing a pos it will default to a interger, looking somewhat like:

matchPlayers = {
[1] = playerInstance1,
[2] = playerInstance2}

When you use the table.remove() function you need to provide a position. In your code, you provide the position as a player instance, which in the table it doesn’t exist.

What you might need is to use the table.find() function which will provide you the position the player is at and then you can remove the player at that position

If you implement it correctly your code should look like:

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, position)
		end)

		Players.PlayerRemoving:Connect(function(v)
			local position = table.find(matchPlayers, v)
			table.remove(matchPlayers, position)
		end)
	end
end

More Information : table

If you have any other questions feel free to reply to this.

2 Likes