This is the code I am using to rotate the character
local targetPosition = closest.HumanoidRootPart.Position
tower.HumanoidRootPart.CFrame = CFrame.new(tower.HumanoidRootPart.Position, targetPosition)
For a little more context, I am making a Tower Dense Simulator game and I have code above this that finds the closest player and plays an animation to attack, and that all works fine, but for some reason the character is not rotating with this code. I have tried everything I can think of and am going insane. Can someone please help me find out why the character refuses to rotate?
I was referring to the lookAt parameter in CFrame.new. A while back, probably around 2 years ago now Roblox added context detection to CFrame.new and it automatically sets it as lookAt if it recognizes it. Also, no, using the actual lookAt function does not yield a different result. I have not tried the PivotTo method yet, but I will try it later today and let you know. Thanks!
This does the same thing as my original code as CFrame.new now automatically sets its second parameter as the lookAt if it recognizes that that is what the code is trying to do. Thank you for the suggestion though!
That is just one of the many overloads for the CFrame constructor. If you go to the documentation for the one where it creates a CFrame that points to a specific position, you’ll see it says that it has been replaced by the CFrame.lookAt function, which was created more recently, so you should prefer to use .lookAt.
On topic though, have you tried the :PivotTo method?
The lookAt function does not work either from my testing, and I have not tried out the PivotTo method because I am in school as I write this. I will try it when I have time to. Thank you for the suggestion!
No, everything else works and the animation which plays literally the line right before that snippet plays correctly. I also put a bunch of print statements that ensure every part of the code executes and they all print.
A client testing script: Rotating player to face a part on the workspace.
local player=game:GetService("Players").LocalPlayer
local character=player.Character or player.CharacterAdded:Wait()
local root=character:WaitForChild("HumanoidRootPart")
local target=workspace:WaitForChild("Part")
task.wait(5)
local rootPos=root.Position
local targetPos=target.Position
--quick rotation x&z
root.CFrame=CFrame.new(rootPos,Vector3.new(targetPos.X,rootPos.Y,targetPos.Z))