How to make pathfinding NPC face player

The title pretty much explains it. I have an NPC with some pathfinding logic so changing the HRP Cframe would result in buggy :MoveTo. I decided to try alignorientation but I have no idea how to change the angle of the HRP so that its always facing the target player because right now the NPC isn’t always directly facing the player. Any help will be appreciated!

local function alignorientationSetUp()
	if not Model.HumanoidRootPart:FindFirstChild("AlignOrientation") then
		local AlignOrientation = Instance.new("AlignOrientation")
		AlignOrientation.Parent = Model.HumanoidRootPart
		AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
		AlignOrientation.Attachment0 = Model.HumanoidRootPart.RootAttachment
		AlignOrientation.MaxTorque = 5000
		AlignOrientation.Responsiveness = math.huge
		AlignOrientation.PrimaryAxisOnly = false
		return AlignOrientation
	end	
	return Model.HumanoidRootPart:FindFirstChild("AlignOrientation")
end

local function CreatePath(PlayerPosition) 
	path:ComputeAsync(Model.PrimaryPart.Position, PlayerPosition)
end

local BlockedConnection
local ReachedConnection
local function PathFindToPlayer()
	if CloneRadius then
		CloneRadius:Destroy()
		RoamingDebonuce = false
	end
	if not Target.Character then 
		Target = nil
		return
	end
	local PlayerCharacter = Target.Character or Target.CharacterAdded:Wait()
	local ModelPosition = Model.PrimaryPart.Position
	local PlayerPosition = PlayerCharacter.HumanoidRootPart.Position
	local Humanoid = Model:FindFirstChild("Humanoid")

	local success, errormessage = pcall(function()
		CreatePath(PlayerPosition)
	end)
	
	if not success then 
		warn(errormessage)
		return
	end
	
	if (ModelPosition - PlayerPosition).Magnitude >= AGGRORANGE then --Destroy alignedorientation 
		Target = nil 
		path:Destroy()
		return	
	end
	
	local AlignedOrientation = alignorientationSetUp()
	--AlignedOrientation.CFrame = CFrame.lookAt(Model.HumanoidRootPart.Position, PlayerPosition)
	
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i, waypoint in pairs(waypoints) do
			if not BlockedConnection then 
				BlockedConnection = path.Blocked:Connect(function()
					path:Destroy()
					BlockedConnection:Disconnect()
					BlockedConnection = nil
				end)
			end
			--if (Model.PrimaryPart.Position - PlayerPosition).Magnitude <= STOPPINGRANGE then
				--Humanoid:MoveTo(ModelPosition)  -- Stop moving
				--return
			--end
		
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Model.Humanoid.Jump = true
			end
			Humanoid:MoveTo(waypoint.Position)
			WalkAnimation:Play()
			if not ReachedConnection then
				ReachedConnection = Humanoid.MoveToFinished:Connect(function()
					print("hi")
					ReachedConnection:Disconnect()
					ReachedConnection = nil
				end)
			end
		end
	else
		print("PATH UNSUCCESSFUL")
	end
end

task.spawn(function()
	while true do
		if not Target then 
			FindNearestPlayer()
		else
			if not Model:GetAttribute("Stunned") then 
				PathFindToPlayer()
				meleeHandler()
			end
		end
		task.wait(0.5)
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

You forgot to create two attachments here in your alignorientationsetup() function so the aligning won’t work at all. The attachment in the player your NPC is pathfinding to should be Attachment0 and the attachment in the NPC should be Attachment1.
You are on the right track putting the attachment in the HumanoidRootPart :slight_smile:

1 Like