Table removing not working

I am trying to remove a teleportation part from a table after a player teleports to it. This is for the spawning part of a game.

The table.remove command is not working. It just shows an error.

ServerScriptService.Server Function:86: invalid argument #1 to ‘remove’ (table expected, got Instance) - Server - Server Function:86
(line 86 is the table.remove)

I tried changing around the order of the arguments, nothing worked. I’m confused.

local Parts = game.workspace.GameSpawn:GetChildren()

for i, Player in ipairs(game:GetService("Players"):GetPlayers()) do
	local SelectedPart = Parts[math.random(1, #Parts)]
		
	Player.Character:MoveTo(SelectedPart.Position)
	table.remove(SelectedPart,Parts)
	print(SelectedPart)
end

It errors explains what the issue is. The first argument you’re sending isn’t a table, it’s an Instance.

The first argument should be a table and the second argument should be item’s index (numerical).

Your script should look like this:

local Parts = workspace.GameSpawn:GetChildren()

for i, Player in ipairs(game:GetService("Players"):GetPlayers()) do
    local SelectedPartIndex = math.random(#Parts) -- this is numerical index for a random part in the 'Parts' table
    local SelectedPart = Parts[SelectedPartIndex]
		
	Player.Character:MoveTo(SelectedPart.Position)
	table.remove(Parts, SelectedPartIndex)
	print(SelectedPart)
end
1 Like