Making a non npc face my character using CFrame but only on the Y axis?

I have an npc that I want to face my character.
I found a method of doing this but its not quite working right:

function aim()
	local chunk = math.rad(90)
	if target then
	local lookvec = target.CFrame.LookVector
	local angle = -math.atan2(lookvec.Z,lookvec.X) + chunk
	script.Parent.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Center.Position) * CFrame.Angles(0,angle,0))
	end
end

sometimes the npc looks at my character, and sometimes its looking in the wrong direction.
whats going on and what do I do to fix this?

You haven’t defined the target so send the full script.
Not going to assume what might be what until you provide the full thing.

EDIT:
For aligning in a single axis read this article.

1 Like