The Last Player In The Table Gets Teleported To The Default Spawn Point

So I have this script that runs through all of the players in a table and teleports them to a part. However the last player never spawns at the part and just spawns at the main spawn point (the default spawn point that everyone spawn at when they join the game).

    local function teleportPlayersToGame()
		
		local number = 1
		
		local SpawnPoints = ClonedArena:FindFirstChild("SpawnPoints")
	
		
	
		repeat
		
			print(#plrs)
			local player = plrs[math.random(1,#plrs)]
			for x, plr in pairs(plrs) do
				if plr == player then
					table.remove(plrs,x)
				end
			end
			
			if number == 1 then
				number = number + 1
				if player then
					character = player.Character
					if character then
						character.HumanoidRootPart.CFrame = SpawnPoints.SpawnPoint1.CFrame
						print("moved to spawn point")
						player.playerAmount.Value = 0
						game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
						print("remote fired to player")
	
					end
				end
			elseif number == 2 then
				number = number + 1
				if player then
					character = player.Character
					if character then
						character.HumanoidRootPart.CFrame = SpawnPoints.SpawnPoint1.CFrame
						print("moved to spawn point")
						player.playerAmount.Value = 0
						game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
						print("remote fired to player")
	
					end
				end
			elseif number == 3 then
				number = number + 1
				if player then
					character = player.Character
					if character then
						character.HumanoidRootPart.CFrame = SpawnPoints.SpawnPoint1.CFrame
						print("moved to spawn point")
						player.playerAmount.Value = 0
						game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
						print("remote fired to player")
	
					end
				end
			elseif number == 4 then
				number = number + 1
				if player then
					character = player.Character
					if character then
						character.HumanoidRootPart.CFrame = SpawnPoints.SpawnPoint1.CFrame
						print("moved to spawn point")
						player.playerAmount.Value = 0
						game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
						print("remote fired to player")
	
					end
				end
			elseif number == 5 then
				number = number + 1
				if player then
					character = player.Character
					if character then
						character.HumanoidRootPart.CFrame = SpawnPoints.SpawnPoint1.CFrame
						print("moved to spawn point")
						player.playerAmount.Value = 0
						game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
						print("remote fired to player")
	
					end
				end
			end
			
			
				
		until #plrs == 0 
		
	end
	
	teleportPlayersToGame()

Do you have the default spawnpoint in the folder?

No (I’ve checked like 5 times)

Do you have enough spawn points for the people that are joining?

Yeah there are 5 spawn points and no more than 5 players are in the table.

Hmm, there doesn’t seem to be any problems with the server script, what does the local script do?

game.ReplicatedStorage.WaitScreenEndBang:FireClient(player) this script?

By the way, these number variables are not necessary, you could just use a for loop.

Yeah, I tried a for loop but the same thing happened so I tried this

Just swap back to the for loop, for just more cleanness, and also try debugging.

1 Like

I switched it to this and all of the prints print 5 times. print(#plrs) prints 5 then 4 all the way down to 1 like it’s supposed to. How else would I try to debug it?

local SpawnPoints = ClonedArena:FindFirstChild("SpawnPoints")
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()
	
		repeat
		
			print(#plrs)
			local player = plrs[math.random(1,#plrs)]
			for x, plr in pairs(plrs) do
				if plr == player then
					table.remove(plrs,x)
				end
			end
			
			if player then
				character = player.Character
			
				if character then
					character.HumanoidRootPart.CFrame = AvailableSpawnPoints[1].CFrame
					print("moved to spawn point")
					player.playerAmount.Value = 0
					game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
					print("remote fired to player")
	
				end
			end
		until #plrs == 0

No use like breakpoints to try and figure out where it crashes at.
If you put a breakpoint where it tries to teleport, then just keep stepping to see where it stops and has a bug or whatever.

1 Like

It doesn’t even let me use any of the step buttons, is there something you have to disable. I have lua debugger enabled.

Uhh, do you have breakpoints set?

Yeah, I only have one.

Annotation 2020-08-08 135954