Making it so pathfinding looks towards a certain direction after finishing path

I need help, im making an ai that i want to be able to face a certain direction after its done its path. Im using the basic roblox tutorial ai.


this is whats currently hapenning. I want it so that when its done moving it faces towards the shelves. Like this


Is there any way i can do that?

forgot to add the script:

-- Variables --

local PathFolder = workspace.PathLocations
local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")


local path = Pathfinding:CreatePath({
	AgentHeight = 6;
	AgentRadius = 3;
	AgentCanJump = false;
	
	Cost = {
		Water = 100;
		DangerZone = math.huge
	}
})

local Character = script.Parent
local humanoid = Character:WaitForChild("Humanoid")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local random = math.random(1,35)
local route = "p"..random
local locationRoute = PathFolder:WaitForChild(route)
local Location = locationRoute.Position

local function followPath(destination)
	
	local success, errormessage = pcall(function()
		path:ComputeAsync(Character.PrimaryPart.Position, destination)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				followPath(destination)
			end
		end)
		
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)

				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		
	end
end

while task.wait() do
	followPath(Location)
end

its just the roblox tutorial ai i modified slightly

1 Like

When the NPC finishes the path, you could set the CFrame (or tween it to look better) of it’s HumanoidRootPart to face the shelves.

The simplest way I could think for you to do this would be to place a part where you want the NPC to look, like this:


(sorry about the random stuff)

From there, tween it like this:

local TweenService = game:GetService("TweenService")

TweenService:Create(Character.HumanoidRootPart, TweenInfo.new(0.5), {CFrame = CFrame.lookAt(Character.HumanoidRootPart.Position, locationRoute.LookAtPoint.Position)}):Play()

This isn’t perfect, but it’ll get you started. With this, add a part called LookAtPoint to each location in the path folder where you want the NPC to look, and run it when the path finishes.

1 Like

Your code doesnt work, but im definitely trying your idea out. Thanks!

1 Like

Nvm, I put your code at the wrong part of the script. Oops… It works now tho.

1 Like

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