Trying to make NPC walk in the direction of a part

local part = workspace.Part
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local pathfinding = game:GetService("PathfindingService")

local function move(pos1,pos2,value)
    local jump = value
    local path1 = pathfinding:CreatePath()
    path1:ComputeAsync(pos1.Position, pos2)
    local waypoints = path1:GetWaypoints()
    for _, waypoint in pairs(waypoints) do
        if waypoint.Action == Enum.PathWaypointAction.Jump and jump == true then
            humanoid.Jump = true
        end
        humanoid:MoveTo(waypoint.Position)
        humanoid.MoveToFinished:Wait(2)



    end    
end

move(humanoidrootpart,part.CFrame.LookVector*10)

I am trying to make an NPC walk in the same direction a part is facing using LookVector.

For some reason, the NPC walks in the wrong direction when the game is run.

How do I fix it so that the NPC walks towards the direction the part is facing?

Do you mean that the part is stationary and the NPC is walking in the direction that the part is facing?

Yes. The npc is walking straight in the direction the part is facing.

Well, the NPC tries to find the best way possible to complete its path in the lowest time, this is why I think that it does not move towards the direction the path is facing

‘’ At its core, a pathfinding method searches a graph by starting at one vertex and exploring adjacent nodes until the destination node is reached , generally with the intent of finding the cheapest route.’’

1 Like

How would I get it to move in the direction the part is facing? Would I still use pathfinding?


So do you want for example the number 2 to go to the direction where the 1 is looking? Or do you want the number 2 to look number 1 while it is moving?

I would like the NPC to go in the direction the front of the part is facing, like this:


The red is the front of the part.

1 Like

Well, I do not think that you can do this with pathfinding alone. What you could do, is to make the dummy to look at the part, but stop at an invisible point that is x far from your part. The problem with this though, is that if the part changes position, the invisible point should change too. I am afraid that I am not aware of any other possible solutions.

1 Like

Thanks for the help! I figured out the issue and decided to use Instance.new to solve it.

For future reference:

local Part = workspace.Part
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local pathfinding = game:GetService("PathfindingService")


local function move(pos1,pos2,value)
	local jump = value
	local path1 = pathfinding:CreatePath()
	path1:ComputeAsync(pos1.Position, pos2)
	local waypoints = path1:GetWaypoints()
	for _, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump and jump == true then
			humanoid.Jump = true
		end
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait(2)



	end	
end

local Direction = Instance.new("Part")	-- Creates the part NPC is going to.
Direction.Position = humanoidrootpart.Position
Direction.Orientation = Part.Orientation
Direction.CFrame = Direction.CFrame + (Part.CFrame.LookVector*20) -- NPC Moves 20 studs in the part's direction.
Direction.CanCollide = false
Direction.Anchored = true
Direction.Transparency = 1
Direction.Parent = workspace

move(humanoidrootpart, Direction.Position)
Direction:Destroy() -- Cleanup
5 Likes