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?
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--).
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)
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.
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)?
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.
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.
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
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.
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.