Trying to teleport players to a part when they spawn but the orientation is wrong each time which causes the player to face the wrong way

Hey everyone! I’m here today as I’m learning lua :smiley:

So, I’m trying to teleport players back to the checkpoint which is equivalent to their leaderstat value. I’m running into a problem with the player orientation which is causing the player to constantly spawn facing one way.

	player.CharacterAdded:connect(function(character)
	local goto = checkpoints[checkpointStat.Value]
	if goto then
		repeat wait() until character.Parent
		character:MoveTo(goto.Position)
	else
		warn("Checkpoint " .. checkpointStat.Value .. " not found")
	end
end)

end)

Any solutions are greatly appreciated.

Thanks

player.CharacterAdded:Connect(function(character)
    local goto = checkpoints[checkpointStat.Value]

	if goto then
	    character:SetPrimaryPartCFrame(goto.CFrame * CFrame.Angles(25, 0, 0))
	else
	    warn("Checkpoint " .. checkpointStat.Value .. " not found")
    end
end)

You will have to change the angles depending on which way you want the player to be facing.

You can make a LocalScript inside StarterPlayerScripts and add this code that makes it always look to a specific side when the character spawns

local localPlayer = game.Players.LocalPlayer

localPlayer.CharacterAdded:Connect(function(char)

wait(0.2)

game.Workspace.CurrentCamera.CFrame = char.Head.CFrame:toWorldSpace() + Vector3.new(0,2,-5) --Offset

game.Workspace.CurrentCamera.Focus = char.Head.CFrame

end)

Is there any way to face players depending on what way the front of each checkpoint is facing?

Try this :slight_smile:

player.CharacterAdded:Connect(function(character)
		local goto = checkpoints[checkpointStat.Value]
		local NextGOTO = checkpoints[checkpointStat.Value + 1]

		
		
		
		if goto then
			local UpperTorso = character:WaitForChild("UpperTorso")
			--character.UpperTorso.CFrame = goto.CFrame + Vector3.new(0, 10, 0)
			
			
			character:SetPrimaryPartCFrame(NextGOTO.CFrame * CFrame.Angles(25, 0, 0))
			

		else
			warn("Checkpoint " .. checkpointStat.Value .. " not found")
		end
end)

Here is a script I made that has a function you can call to teleport any character to any part, and it will face the character to the forward direction the part is facing (based on the rotation around the Y axis of the part. I tested it quickly in studio and it seems to work correctly. This will ideally be in a server script:

local checkpoints = workspace.Checkpoints --folder of the checkpoint parts

function TeleportCharacterToPart(character: Model, part: BasePart)
	local checkpointRotation = part.Orientation.Y
	character:MoveTo(part.Position)
	local characterCf = CFrame.new(character:GetPrimaryPartCFrame().Position) * CFrame.Angles(0, math.rad(checkpointRotation), 0)
	character:SetPrimaryPartCFrame(characterCf)
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	--Teleport players when they spawn to their current checkpoint
	player.CharacterAdded:Connect(function(character: Model)
		character:WaitForChild("HumanoidRootPart")
		task.wait()
		
		local checkpoint = player.leaderstats.Checkpoint.Value --refers to their checkpoint value
		local checkpointPart = checkpoints:FindFirstChild(checkpoint)
		if checkpointPart then
			TeleportCharacterToPart(character, checkpointPart) --teleport player to checkpoint
		end
	end)
end)

--somewhere else in code in this script whenever you want to TP them to a checkpoint:
TeleportCharacterToPart(character, somePart) --example