How to rotate a player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    I want to rotate a player 180° when he gets teleported

  2. What is the issue? Include screenshots / videos if possible!

    Whenever I try to use CFrame.Angles the player gets teleported somewhere and not rotated

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    Using CFrame.Angles, setting the cframe to a new cframe of the current position of the player + CFrame.Angles

--jmp1 and jmp2 are characters of the players
jmp1:SetPrimaryPartCFrame(CFrame.new(141.275, 567.343, -320.41))    --jmp's get teleported
jmp2:SetPrimaryPartCFrame(CFrame.new(141.275, 567.343, -373.633))
--I need the rotation to happen here to jmp2 by 180°

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

jmp1:SetPrimaryPartCFrame(jmp1:GetPivot() * CFrame.AngleS(math.rad(180),0))  
jmp2:SetPrimaryPartCFrame(jmp2:GetPivot() * CFrame.AngleS(math.rad(180),0))

Try this

@This100Times

1 Like

try it in this way, I’ll give you two codes, one with TweenService and other without, cause using tweenservice can help you make it look like it was smooth.

Without TweenService: (it instantly turns the player 180 degrees)

local hrp1=jmp1:FindFirstChild("HumanoidRootPart")
local hrp2=jmp2:FindFirstChild("HumanoidRootPart")

if hrp1 and hrp2 then
	hrp1.CFrame=hrp1.CFrame * CFrame.Angles(0,math.rad(180),0)
	hrp2.CFrame=hrp2.CFrame * CFrame.Angles(0,math.rad(180),0)
end

Using TweenService: (Its smooth, you can adjust the time taken and tween styles using tween info parameters)

local hrp1=jmp1:FindFirstChild("HumanoidRootPart")
local hrp2=jmp2:FindFirstChild("HumanoidRootPart")

local tweenService=game:GetService("TweenService")
local tweenInfo=TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
if hrp1 and hrp2 then
	tweenService:Create(hrp1,tweenInfo,{CFrame=hrp1.CFrame * CFrame.Angles(0,math.rad(180),0)}):Play()
	tweenService:Create(hrp2,tweenInfo,{CFrame=hrp2.CFrame * CFrame.Angles(0,math.rad(180),0)}):Play()
end

this should help! let me know if theres something wrong.

2 Likes

This doesn’t work. It says “Attempted to call a nil value”. Might be because its a server script, not a local one

jmp1:PivotTo(jmp1:GetPivot() * CFrame.Angles(math.rad(180),0))  
jmp2:PivotTo(jmp2:GetPivot() * CFrame.Angles(math.rad(180),0))

For some reason the players aren’t rotated here and are facing the same direction(as before), but there is no error.

Again, no error but the player’s are facing the same as before

Oh you should have specified that it was a firstperson game, what you could do is, use a LocalScript to just manipulate the camera to turn 180 degrees and it should work. Cause its in firstperson, it should also rotate the character with camera.

Use remote events, and do :FireClient() and use this code in localscript using OnServerEvent :

workspace.CurrentCamera.CFrame = CFrame.Angles(0, math.rad(180), 0) * workspace.CurrentCamera.CFrame

because the scripts I gave you earlier do not work if the player is in firstperson or has shiftlock turned on.

1 Like
jmp1:PivotTo(jmp1:GetPivot() * CFrame.Angles(0,0,0))  
jmp2:PivotTo(jmp2:GetPivot() * CFrame.Angles(0,math.rad(180),0))

try this

Actually though, you could just rotate the part that is jmp1 to be opposite in rotation to jmp2 in studio

Won’t work, cause as you see, he is in firstperson, manipulating CFrame won’t work as the client side is constantly overriding the CFrame

1 Like

I did not know that, learned something new I guess.
I don’t usually work in first person.

I will do a quick bit of experimenting and see if I can find a solution.

Ok so I tried this and I made the script print out which player rotated so Im sure it started but it didn’t rotate him again

Maybe it has something to do when I made the camera scriptable?
I could maybe also do something like this but I’m not sure it’ll work.
My camera is fixed to the head of the player so I maybe if I move the camera it’ll also move the head?

local camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local character = Player.Character
local head = character:WaitForChild("Head")
camera.CoordinateFrame = CFrame.new(head.Position) * CFrame.Angles(0,math.rad(180),0)

Ok, nope, but there is no error though

Yeah also, if you are using any framework using RunService to keep changing the cameras CFrame, it’ll be overriding anything else as RenderStepped runs like what, 60 times a second?, that might be why?

Yea I’m using a RunService, I’ll try to find a workaround in it if there are any or just make a new first person script

Wait, If I teleport rotate the players before they go into the first person script, it should rotate them as normally

You can rotate the camera 180 degrees using the same script you are using RunService in.
Heres one way:
Define a variable, lets say:

local Rotate180Degrees = false

and using OnServerEvent, you turn the value Rotate180Degrees to true.

In the RunService function, what you do is,

if Rotate180Degrees == true then
    Rotate180Degrees = false
    workspace.CurrentCamera.CFrame = CFrame.Angles(0, math.rad(180), 0) * workspace.CurrentCamera.CFrame
end

You get what I mean?

1 Like

Ok, I got it to work, what I did was set an attribute on the character called ‘Teleport’ and in a local script I check for this attribute being changed, and when it is changed, I set the camera cframe from it

I will send a place file

Teleporter.rbxl (58.4 KB)

image

image

local start = script.Parent
local beam = start:WaitForChild("Beam")
local stop = beam.Attachment1.Parent
local decal = stop:WaitForChild("Decal")

local debounce = false
start.Transparency = 1
stop.Transparency = 1
beam.Enabled = false
decal.Transparency = 1

start.Touched:Connect(function(part)
	if not debounce then
		debounce = true
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player and part.Parent:FindFirstChild("Humanoid") then
			local char = part.Parent
			local hrp = char.PrimaryPart
			local humanoid = char.Humanoid
			local cf = stop.CFrame + Vector3.new(0,(stop.Size.Y/2)+(hrp.Size.Y/2)+humanoid.HipHeight ,0)
			char:SetAttribute("Teleport",cf)
			char:PivotTo(cf)
			wait(.3)
		end
		debounce = false
	end
end)

image

script.Parent:GetAttributeChangedSignal("Teleport"):Connect(function()
	local cf = script.Parent:GetAttribute("Teleport")
	script.Parent:PivotTo(cf)
	workspace.CurrentCamera.CFrame = cf
end)
1 Like