PivotTo only works once?

I’m making a map transition script that teleports you to a seperate lobby then back to a new map, and it works fine the first try, but the next time it transitions to a new map it doesnt teleport the players to the lobby. It still loads the map and even prints in the loop to get all characters in the session, what is causing this?

Here is the code:

game.Players.PlayerAdded:Connect(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		local torso = character:WaitForChild("Torso")

		if humanoid and torso then
			table.insert(charactersInSession, character)
		end
	end

end)

task.spawn(function()
	while task.wait() do
		local chooseMap = math.random(1,#mapInventory)
		local chosenMap = mapInventory[chooseMap]:Clone()
		chosenMap.Parent = game.Workspace.Ingame
		for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			player:LoadCharacter()
		end
		task.wait(MATCH_TIME)
		--repStorage.Events.MapTransition:FireAllClients()
		task.wait(MAP_CLEAR_TIME)
		for _, character in ipairs(charactersInSession) do
			if character then
				character:PivotTo(workspace.Lobby.SpawnPart.CFrame)
			end
		end
		game.Workspace.Ingame:ClearAllChildren()
		task.wait(MAP_LOAD_TIME)
	end
end)

Idk what’s causing the issue, but have you tried using SetPrimaryPartCFrame? It’s worked for me a lot

I figured out the issue, I had already tried primarypartcframe but the reason it wasn’t working was cause I wasn’t clearing the array before teleporting again, so I guess it wouldn’t tp players who already had been teleported.

local charactersInSession = {}

function addPlayersInSession()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		local torso = character:WaitForChild("Torso")

		if humanoid and torso then
			table.insert(charactersInSession, character)
		end
	end
end

function clearPlayersInSession()
	table.clear(charactersInSession)
end

task.spawn(function()
	while task.wait() do
		local chooseMap = math.random(1,#mapInventory)
		local chosenMap = mapInventory[chooseMap]:Clone()
		chosenMap.Parent = game.Workspace.Ingame
		for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			player:LoadCharacter()
		end
		task.wait(MAP_MATCH_TIME)
		repStorage.Events.MapTransition:FireAllClients()
		task.wait(LOBBY_TELEPORT_TIME)
		addPlayersInSession()
		for _, character in ipairs(charactersInSession) do
			if character then
				print("pivoting")
				character:PivotTo(workspace.Lobby.SpawnPart.CFrame)
			end
		end
		game.Workspace.Ingame:ClearAllChildren()
		task.wait(LOBBY_WAIT_TIME)
		clearPlayersInSession()
	end
end)
1 Like