Why won't my pathfinding script work?

I made a pathfinding script for an NPC. I followed the Pathfinding tutorial and made sure to follow every step precisely. When I finished it, it worked perfectly fine, it did everything I wanted it to. Then I hopped off Studio for the night.

When I went back on this morning and launched the NPC, it all of the sudden won’t bother going around obstacles and walks straight to the target. I didn’t tamper with the script or NPC at all, I just closed my laptop for the night. I’ve tried digging through the script and output looking for errors, but none were there.

I am very sorry if I overlooked an obvious thing, but I just didn’t see anything. Help would be greatly appreciated. Here is the script:

local PathfindingService = game:GetService("PathfindingService")

local motivator = game.Workspace.motivator
local humanoid = motivator.Humanoid
local destination = game.Workspace.GreenFlag

local path = PathfindingService:CreatePath()

path:ComputeAsync(motivator.HumanoidRootPart.Position, 
destination.PrimaryPart.Position)

local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
    local part = Instance.new("Part", game.Workspace)
    part.Shape = "Ball"
    part.Material = "Neon"
    part.Size = Vector3.new(.6,.6,.6)
    part.Position = waypoint.Position
    part.Anchored = true
    part.CanCollide = false

    humanoid:MoveTo(waypoint.Position)
    humanoid.MoveToFinished:Wait()
end

Make sure the obstacles are added before the path is computed. If that is the case than consider adding wait before computing path.
Edit: @opticthunder
I tested your code in studio and it seems to work.
test

Here is the code,

local PathfindingService = game:GetService("PathfindingService")
local motivator = game.Workspace.motivator
local humanoid = motivator.Humanoid
local destination = game.Workspace.GreenFlag

local path = PathfindingService:CreatePath()

path:ComputeAsync(motivator.HumanoidRootPart.Position,destination.PrimaryPart.Position)

local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
    local part = Instance.new("Part", game.Workspace)
    part.Shape = "Ball"
    part.Material = "Neon"
    part.Size = Vector3.new(.6,.6,.6)
    part.Position = waypoint.Position
    part.Anchored = true
    part.CanCollide = false

    humanoid:MoveTo(waypoint.Position)
    humanoid.MoveToFinished:Wait()
end

I tried adding “wait()” and putting obstacles first but it didn’t work either.

I deleted the original motivator, put in a new one, copied your comment’s script, and it still wouldn’t go around walls.

It worked before I went to sleep. I don’t know why it’s broken.

A video would be really helpful. Also, have you tried saving the game or look in the output?

1 Like

Tried saving the game and it didn’t work and there was nothing in the output. Here’s a gif:

AIgif

It looks like the straight path is shorter than around the red part because the model can jump the obstacle if the jump property is set to true.

1 Like

Yes! You found it. I made the walls higher and the AI walked around it perfectly. I even tested it by surrounding the flag by smaller walls after the AI walked around a tall wall.

I’m not experienced with pathfinding (I started 2 days ago), this never even crossed my mind. Thank you so much!