Cannot teleport players

  1. What do you want to achieve? I am trying to teleport multiple players and scatter them across multiple parts.

  2. What is the issue? Everything goes well until I get to teleporting, and the game does not do anything else. There isn’t anything in the dev console, so I don’t know what went wrong.

  3. What solutions have you tried so far? I tried putting in .Position for each teleport part, but that does not work either.

Below is the current code I have:

for i, player in pairs(plrs) do
		if player then
			local name = player.Name
			local character = game.Workspace:WaitForChild(name)
			
			if character then
				-- Teleport them
				local root = character:FindFirstChild("HumanoidRootPart")
				
				root.Position = availableSpawnPoints[1].Position -- the part I suspect is the problem
				table.remove(availableSpawnPoints,1)

Thank you for any help!

Can you show the code above it that adds the parts into the availableSpawnPoints table?

Try setting the CFrame directly rather than the position, like this.

root.CFrame = CFrame.new(availableSpawnPoints[1].Position)

I don’t think directly setting the position like you’re trying to do works.

I don’t think there is a table… Sorry, I followed this from a tutorial, and I don’t remember adding another table. What should I do to add one?

The availableSpawnPoints is a list of the parts that are available. Can you send the rest of the script? (the stuff above what you sent and the rest of it)

It’s pretty long, but here it is:

-- Define Variables

local replicatedStorage = game:GetService("ReplicatedStorage")

local serverStorage = game:GetService("ServerStorage")

local mapsFolder = serverStorage:WaitForChild("Maps")

local Status = replicatedStorage:WaitForChild("Status")

local gameLength = 120

local reward = 1

-- Game loop

while true do
	
	Status.Value = "Waiting for players"
	
	repeat wait(1) until game.Players.NumPlayers >= 2
	
	Status.Value = "A new game is starting..."
	
	wait(15)
	
	local plrs = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into the table
		end
	end
	
	wait(2)
	
	local availableMaps = mapsFolder:GetChildren()
	
	local chosenMap = availableMaps[math.random(1,#availableMaps)]
	
	Status.Value = chosenMap.Name.." Map Chosen"
	
	local clonedMap = chosenMap:Clone()
	clonedMap.Parent = workspace
	
	-- Teleport players to the chosen map
	
	local SpawnPoints = clonedMap:FindFirstChild("SpawnPoints")
	
	if not SpawnPoints then
		print("SpawnPoints not found")
	end
	
	local availableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			local name = player.Name
			local character = game.Workspace:WaitForChild(name)
			
			if character then
				-- Teleport them
				local root = character:FindFirstChild("HumanoidRootPart")
				
				root.CFrame = CFrame.new(availableSpawnPoints[1].Position)
				table.remove(availableSpawnPoints,1)
				
				
				-- Give out swords
				
				local Sword = serverStorage.Sword:Clone()
				Sword.Parent = player.Backpack
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player
				
			else
				-- There is no character
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end
	
	
	Status.Value = "Get ready to play!"
	
	wait(2)
	
	for i = gameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				local name = player.Name
				local character = game.Workspace:WaitForChild(name)
				
				if not character then
					-- Left the game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.." is still in the game")
					else
						-- They are dead
						table.remove(plrs,x)
						print(player.Name.." has been removed")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.." has been removed")
			end
		end
		
		Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left! Try to stay alive!"
		
		if #plrs == 1 then
			-- Last person standing
			Status.Value = plrs[1].Name.." has won!"
			plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + reward
			break
		elseif #plrs == 0 then
			Status.Value = "Nobody won..."
			break
		elseif i == 0 then
			Status.Value = "Time's up! Nobody won!"
			break
		end
		
		wait(1)
	end
	
	print("End of game")
	
	for i, player in pairs(game.Players:GetPlayers()) do
		local name = player.Name
		local character = game.Workspace:WaitForChild(name)
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy()
			end
			
			if character:FindFirstChild("Sword") then
				character.Sword:Destroy()
			end
		end
		
		player:LoadCharacter()
	end
	
	clonedMap:Destroy()
	
	Status.Value = "Game over!"
	wait(2)
end

If it helps, SpawnPoints is in game.ServerStorage.Maps.ClassicMap.SpawnPoints

In the chosen map, make sure you have a folder named SpawnPoints. Within that folder, make sure you have PARTS that are the spawn points.

Also, replace that line with:

root.CFrame = availableSpawnPoints[1].CFrame
2 Likes

It works! Thank you so much for the help!