LookAt() but only change rotation?

So far I have a script that has an NPC following a player, however I want the NPC to fully turn and look at the player with their whole body.

		script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(
			script.Parent.HumanoidRootPart.Position,target.Position*Vector3.new(1,0,1)
			+ script.Parent.HumanoidRootPart.Position*Vector3.new(0,1,0)
			))

This is what I’m using to have the NPC turn, and it works technically but I also would like for them to not teleport around?

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and game.Players:GetPlayerFromCharacter(human.Parent) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(.5)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(
			script.Parent.HumanoidRootPart.Position,target.Position*Vector3.new(1,0,1)
			+ script.Parent.HumanoidRootPart.Position*Vector3.new(0,1,0)
			))
		script.Parent.Humanoid:MoveTo(target.Position-Vector3.new(-4,-4,-4), target)
	end
end

Here is the whole script and here is what’s happening right now.


I understand why it’s doing this but I don’t know how to change it.

You can subtract out the position from the CFrame and add back the position you want it to actually be at.

Can you explain a little more? I don’t get it. What CFrame am i subtracting from, and wouldn’t doing this still have the NPC teleport around?

I think the teleporting is unrelated to the actual CFrame you’re using, it is probably because setting a CFrame bypasses interpolation so it will always look jumpy if it is sent over the network. What I was thinking is you can add/subtract a vector from a CFrame to remove its position offset and replace it with a different one: cf - cf.Position + newPositionVector

I’m still confused, I make the new position equal to the old cf vector plus the new one?

try using an AlignOrientation. the AlignOrientation uses physics sp its much smoother

Oh awesome! I see it and it looks promising, i would just make two attachments and put one in the npc and one on the player?