The Last Player In A Table Does Not Get Teleported

This script is supposed to teleport all of the player from the table, however the last player in the table does not get teleported

This part is working, it prints both players in the table

    local plrs = {}
game.ReplicatedStorage.AddPlayerToTable.OnServerEvent:Connect(function(player)
	--for i, player in pairs(plrs) do
		--if not player then
		table.insert(plrs,player)
	        print(plrs)
		--end
	--end
		
	end)

In this part of the script it also prints both players but the last player in the table does not get looped through the for loop

if #plrs == 2 then
print(table.unpack(plrs))
local map = game.ReplicatedStorage:FindFirstChild(“Arena”)
local clonedMap = map:Clone()
clonedMap.Parent = workspace
local SpawnPoints = clonedMap:FindFirstChild(“SpawnPoints”)
local AvailableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs(plrs) do
		
		if player then
			character = player.Character
			
			if character then
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
				table.remove(AvailableSpawnPoints,1)
				table.remove(plrs,i)
				game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
				
			else
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end
	
end

Does anyone know what is happening here? Thanks!

1 Like

When you print plrs, it will say table: randomlettersandnumbers. Does it print that the length of the table is 2 after the conditional statement?

Try print(table.unpack(plrs) See if it print out somethiing different.

I’d say let’s rewrite this code and make it easier to work with. Remember to make it clear what each snippet of your code does!

I don’t know why you’re having the client tell the server to add them to the plrs table, but I won’t do that in the following code.

My version

function SpawnMapAndTeleportPlayers(Map)
	local Players = game.Players:GetPlayers()
	
	-- I don't fully know your intentions so I won't check for if the Character exists, this is up to you to add if necessary
	
	--||	Clone and parent map
	Map = Map:Clone()
	Map.Parent = workspace

	--||	Teleport players
	local SpawnPoints = Map.SpawnPoints:GetChildren()

	for i, player in pairs(Players) do
		local spawn = SpawnPoints[i] -- assuming that there are enough spawn points for each player
		
		-- Teleport player
		
	end
end

If you want me to help you out further, then I’ll need you to explain what exactly you’re wanting to do and what you need help with.

1 Like

What I’m trying to do: The player picks a character to play as, then they wait (there’s a wait screen) for more players to join. Once there are 5 players they get teleported to the map (wait screen goes away).

Problem: The 6th player does not get teleported and is still in the wait screen.

I used your script and changed it a little

local plrs = {}
game.ReplicatedStorage.AddPlayerToTable.OnServerEvent:Connect(function(player)
	--for i, player in pairs(plrs) do
		--if not player then
		table.insert(plrs,player)
	
		--end
	--end
		
end)


function SpawnMapAndTeleportPlayers(Map, player)

	Map = game.ReplicatedStorage.Arena:Clone()
	Map.Parent = workspace
	
	local SpawnPoints = Map.SpawnPoints
	local AvailableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
				local character = player.Character
				
				if character then
					character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
					table.remove(AvailableSpawnPoints,1)
					table.remove(plrs,i)
				game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
			end
		end
		
	end
end

while true do 
	if #plrs == 2 then
		SpawnMapAndTeleportPlayers()

	end
	wait()
end

You’re still using table methods, though. Which I specifically tried not to use :stuck_out_tongue:

In addition, you added if player then. This serves no purpose as it’d always be true considering the context. If you’re wanting to check if the player exists, then you need to check if its parent is nil.

If I don’t put the players in a table then how do I teleport only certain players and not all of the players in the game?

You could i.e. make a “ready” table for this.

local ReadyPlayers = {}

Ready.OnServerEvent:Connect(function(Player)
	ReadyPlayers[Player] = true
end)

To check if the player is ready, you’d just do something like:

if ReadyPlayers[Player] then
	-- do something here
end
1 Like

Isn’t that just checking to see if one player is ready? How would I check to see if 6 players are ready?

for i, Player in pairs(game.Players:GetPlayers()) do
	if ReadyPlayers[Player] then
		-- code
	end
end

Won’t the code still run if there is only 1 player? I don’t understand where it’s checking to see if there are 6 players.

You just need to be a bit creative. I misunderstood your question apparently, but to check how many players there are, you can just make a function like so:

function GetTableLength(Table)
	local Length = 0
	for i, v in pairs(Table) do
		Length += 1
	end
	return Length
end

P.S. I’m trying not to make it all for you, but helping you with parts of it. I’m on purpose not putting everything together.

1 Like

It prints the randomlettersandnumbers then (x2)

It prints Player2 Player1

charrrrr

Is the Player2 and Player1 an object or the name?

It’s skipping some players in the list because you are making the list shorter while looping through the same list.

local t = {"A","B","C","D","E"} 
for i,v in pairs(t) do 
	if v == "C" then 
		table.remove(t, i) 
	end 
	print(i,v) 
end

Output:

1 A
2 B
3 C
4 E

See how there was 5 entries, but it only prints 4 times?

What you need to do is instead of removing players from the list, to simply just teleport as you go down the list

if character then
	character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[i].CFrame
	game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
end
1 Like