How do I Make the Player's Camera Match the Part's Orientation when using :PivotTo()

Hey everyone! So, I have this script that teleports all players to a part, which works as intended. However, I also want them to face the same direction as the part. How would I implement that in this code snippet?

if lobby:FindFirstChild("Spawn") then
	local c = game.Players:GetChildren()

	for i = 1, #c do
		if c[i].Character ~= nil and c[i].Character:FindFirstChild("HumanoidRootPart") then
			c[i].Character:PivotTo(lobby.Spawn.CFrame) -- how do i make the player face the right way?
		end
	end
end

Heres what I expect the script to do:

What it actually does:

I am aware I can just rotate the entire map but I would prefer to make the player face the correct way when entering the level.

If you’re matching the player character’s CFrame to the spawner, then you can just physically rotate the spawner so that it faces the direction that you want the player to face.

I have tried rotating it, but it keeps facing the same way. But since this is the player’s character model, I am not really sure how to rotate the player so they face the correct direction.

Are you certain that the code is even executing? If you set one object’s CFrame to another’s, they’ll match their rotation and position. So rotating the spawner would also rotate the player. Or there could be a timing issue, but that’s unlikely.

I am sure the code is executing. If it didn’t, then I wouldn’t even spawn in the level to begin with. Also, I put a print statement just in case and it did execute.

Well it should definitely rotate if you rotate the spawner. But you can always just apply a rotational offset. Just do:

c[i].Character:PivotTo(lobby.Spawn.CFrame * CFrame.Angles(0,math.rad(90),0))

Have you tried changing lobby.Spawn.CFrame to lobby.Spawn:GetPivot()?

It seems to be rotating the player’s model, but its not affecting the camera.

Let me try to see if it does anything.

Still faces the same direction…

Either your script isn’t working correctly or you’ve got a conflict with something else. You could try adding a slight delay before moving the character and that would be a hacky way of fixing timing-related issues.

Well, since this is a first-person game, how about rotating the camera after setting the character’s CFrame?

camera.CFrame = CFrame.new(camera.CFrame.Position) * SpawnLocation:GetPivot().Rotation

1 Like

I should let you know that this is a serversided script. How do I get the player’s camera in the for do loop?

you can do this

player.Character.HumanoidRootPart.CFrame = lobby.Spawn.CFrame

might not work but its best to give it a try

I mean it rotates the character, but I want it to also update the player’s camera.

oh in that case then I’m out of ideas, because my other idea was to make it so that the character refresh (loadcharacter), then teleport them to the part

1 Like

After some testing, it seems that nothing is broken. All you need to do is set the CFrame of the camera too. You can either set it the same way as the character, or you can give it that CFrame.Angles(0,math.rad(90),0) offset.

1 Like

Ok so after some tweaking, I have found the solution to my question. Basically, I decided to use a remote event to set the camera to the part’s CFrame while using :PivotTo() to teleport the player.

ServerSided Script:

for i = 1, #c do
	if c[i].Character ~= nil and c[i].Character:FindFirstChild("HumanoidRootPart") then
		c[i].Character:PivotTo(lobby.Spawn.CFrame)
		game.ReplicatedStorage.RotateCamera:FireClient(c[i], lobby.Spawn.CFrame)
	end
end

LocalScript:

game.ReplicatedStorage.RotateCamera.OnClientEvent:Connect(function(CFrame)
	game.Workspace.CurrentCamera.CFrame = CFrame
end)
1 Like