How to rotate a player using CFrame

When a player touches a part, I want them to teleport to another location. The problem is that when I tested it I teleported to the part, it worked fine, but I found that the player doesn’t rotate to face forward. I tried using Youtube but it just focused on rotating a part and didn’t work when I tested it.

     local newCFrame = CFrame.new(-764.3, 855, 121.05)

     script.Parent.Touched:connect(function(hit)
          local humanoid = hit.Parent:FindFirstChild("Humanoid")
          if humanoid then
	          local humanoidRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
	          if humanoidRoot then
		          humanoidRoot.CFrame = newCFrame
      	      end
           end
       end)

This is the script that I made to teleport the player, but as I stated before, it doesn’t rotate the player 90 degrees counter-clockwise so that they’re facing forward. So my question is how would I also rotate the player using CFrame?

1 Like

It seems you aren’t using the Rotation parameter of CFrame. You should set up your CFrame like this: CFrame.new(Vector3.new(-764.3, 855, 121.05), Vector3.new(--ROTATION--) To rotate the player relative to its original rotation, use multiplication as so: Vector3.new(--Original rotation-- * --Other rotation--).

1 Like
local newCFrame = CFrame.new(-764.3, 855, 121.05) * CFrame.fromEulerAnglesXYZ(0, math.rad(90), 0)

script.Parent.Touched:connect(function(hit)
  	local humanoid = hit.Parent:FindFirstChild("Humanoid")
  	if humanoid then
      	local humanoidRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
      	if humanoidRoot then
          	humanoidRoot.CFrame = newCFrame
	    end
   	end
end)
1 Like

A CFrame contains both a Position component and a rotational component. When you have defined the CFrame at the start it just has a position component and nothing to add any kind of rotation.

You can change the rotation of a CFrame by using:

CFrame * CFrame.Angles(0,0,0)

You can then change the rotational component inside the the .Angles(), for example:

CFrame * CFrame.Angles(0, math.rad(90), 0)

to rotate the CFrame by 90 degrees on the Y axis.

You can also have a rotation when creating the CFrame, as the second parameter is the Vector the CFrame is pointing to e.g.

CFrame.new(Vector3.new(0,0,0), Vector3.new(10,0,0)

is a CFrame at position (0,0,0), but facing (10,0,0).

2 Likes

I wasn’t sure how to use the rotation parameter. Would I want to rotate the player relative to its original location, or would the first example be better? Also, how would I put in the original rotation? Would it be like (0, 0, 0) and then the other rotation would be (0, 90, 0)?

1 Like

Pretty sure you can just write:

local newCFrame = CFrame.new(-764.3, 855, 121.05) * CFrame.Angles(0,math.rad(90),0) -- change these values e.g. CFrame.Angles(0,math.rad(180),0)

Instead of

local newCFrame = CFrame.new(-764.3, 855, 121.05)

Let me know if that works, am on mobile :slight_smile:

1 Like

To answer your first question, if you want a definite rotation that isnt affected by the players original rotation, use the first method. If you do use the first method, use CFrame.Angles as @mario118118 said.

To get the players original rotation, get the RootParts CFrame. The rotation is stored under CFrame.LookVector.

1 Like

I tried doing that, but then then either it wouldn’t rotate the player, or nothing would happen at all.

1 Like

Yes, that’s a blank rotation. To change the rotation you can change the numbers withing CFrame.Angles()

For example, try CFrame.Angles(0,math.rad(90),0) as this will rotate the CFrame 90 degrees on the y axis.

Make sure you’re actually multiplying your original CFrame by the CFrame.Angles, not just “CFrame” like in my post (that was an example)

1 Like

I tested it, and it just teleports the player, no rotation at all is happening.

1 Like

Maybe I’ve made a mistake somewhere. What’s the code look like now?

1 Like
local newCFrame = CFrame.new(-764.3, 855, 121.05) * CFrame.Angles(0 ,math.rad(180), 0)

I tried putting different numbers besides 180, and it still doesn’t rotate the player.

1 Like

And there aren’t any errors in the output? Maybe another piece of code is broken, somehow.

I honestly don’t know, I thought that would have worked.

1 Like

image
Thats what it says in the output, but when I clicked on it it wouldn’t lead me to a specific line.

1 Like

Its probably simpler to just use math.pi instead of math.rad(180). (math.pi is a full rotation (180 degrees), math.pi/2 is half a rotation (90 degrees), etc.)

Your script is probably erroring before it gets to where you teleport the player.

1 Like

Well, I don’t think that’s related to this piece of code so that shouldn’t be affecting it in any way.

I don’t really know what else could he happening, unless you can’t rotate a HumanoidRootPart like that for some reason and I’m just unaware of it. I’m afraid I can’t help much more as I’m away from my computer so I can’t try it myself, either.

If anyone else has any ideas feel free to have a go :smiley:

1 Like

Yeah, it appears beforehand.

So my new code is
local newCFrame = CFrame.new(-764.3, 855, 121.05) * CFrame.Angles(0,math.pi(180),0)
Now it won’t teleport the player at all, and I’m aware it could be that I didn’t put a space after each comma but it didn’t work even when I did.

1 Like

math.pi is a constant, no need to call it.

1 Like

I just made what you need, I didnt do it with CFrames but I´m pretty sure you can, I just used the Orientation of the Player´s HumanoidRootPart, like this:

My post works correctly when I tested it. Could it just be because you’re holding the W key while walking into the part? Roblox will automatically rotate your character towards the direction of whatever key you’re holding down.

1 Like