Character Not Rotating With CFrame LookAt

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 thought you were using CFrame.lookAt? Anyways, if replacing doesn’t work then try doing this:

local targetPosition = closest:GetPivot().Position
tower:PivotTo(CFrame.lookAt(tower:GetPivot().Position, targetPosition)

Let me know if it works.

I think an alternative if that does work is this:

local targetPosition = closest.HumanoidRootPart.Position
tower.HumanoidRootPart.CFrame = CFrame.lookAt(tower.HumanoidRootPart.Position, targetPosition)

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! :grin:

1 Like

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! :grin:

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!

Could it be a different issue, like your not looping the code?

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

My character in this case is not a player

Nope, still doesn’t work. :frowning: Here is the place file if you want to look into it.
tds.rbxl (163.0 KB)

So, I tested it in studio, (moved normal enemy into workspace) I ran this command:

game.Workspace.Lebonbon:PivotTo(CFrame.lookAt(workspace.Lebonbon:GetPivot().Position, workspace.Normal:GetPivot().Position))

And, sure enough, Lebron James rotates and looks at the zombie, (tried 2 more times and works)

Can we have the entire script? I know you tested it, but I suspect the issue here is either the order of execution or another lurking bug.

	if attack1style.Value == "Projectile" then

Looks like you missed the .Value a few times…

That’s really strange. I honestly suspect that the animation that plays at the same time as the rotation may be interfering.

Oh my God! Thank you so much. I just realized you’re right. Everything works now, sorry for making such a big fuss about my dumb mistake. :joy:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.