MoveToFinished() fires after the CFrame of the humanoidRootPart is set to look at the player

I want to make it so the NPC always looks at the player when they’re in line of sight

The issue is that, when setting the NPC’s cframe with CFrame.LookAt, MoveToFinished() fires instantly without actually reaching the waypoint + it also stops the NPC’s movement.

Video clip of the issue:

External Media

You can also see how at the output it prints which waypoint it’s currently moving to.
And the function I use to make the NPC look at the player is this:


function lookAt(target)
	
	if workspace:FindFirstChild("PixelRankYT") then
		local lookAtPos = Vector3.new(workspace.PixelRankYT.HumanoidRootPart.Position.X,script.Parent.HumanoidRootPart.Position.Y,workspace.PixelRankYT.HumanoidRootPart.Position.Z)
		script.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position,lookAtPos)
		if curWaypoint > 0 then
			print(curWaypoint) --This is the print which shows which waypoint it's currently moving to
		end
	end
end
2 Likes

I had the same issue. To avoid this problem, use an AlignOrientation on the HumanoidRootPart instead of manually updating/lerping the CFrame of the root part every frame.

Using an AlignOrientation won’t cause MoveToFinish to fire!

1 Like

Oh wow, I completely forgot about this post.
I’ll keep this in mind the next time I make something that requires the rotation of a character with pathfinding, thanks!

2 Likes

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