Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pathfinding - Path.Status breaking script due to it returning "Enum.PathType.NoPath"?

Asked by 2 years ago
Edited 2 years ago

I am creating a restaurant game were people get customers that are their friends running from the client. I am new to pathfinding, so I have been following this guide. However, the guide is confusing me even more, and trying to follow it the code is not working now that I am at the end.

The code has an if statement, and it checks if path.Status == Enum.PathStatus.Success however it is not running because the path.Status is Enum.PathStatus.NoPath Is there a reason that this is happening? I have tried putting vector3.new() around the seats position but it's still not working.

The code [Only the pathfinding part, as I dont believe any other piece is needed.]:

local Target = Mouse.Target
                    Target = Target.Parent.Parent
                    print(Target)
                    if Target:FindFirstChild("Amount") then
                        local Seat = Target:FindFirstChild("Seat",true)
                        print(Seat)
                        local currentWaypointIdx = 1

                        local PathSettings = {
                            AgentRadius = 3.0,
                            AgentHeight = 0.0,
                            AgentCanJump = false,
                        }
                        local waypoints
                        local nextWaypointIndex
                        local reachedConnection
                        local blockedConnection

                        local path = PathService:CreatePath(PathSettings)

                        local function followPath(destination)
                            -- Compute the path
                            local success, errorMessage = pcall(function()
                                path:ComputeAsync(Customer.PrimaryPart.Position, destination)
                            end)

                            if success and path.Status == Enum.PathStatus.Success then
                                -- Get the path waypoints
                                waypoints = path:GetWaypoints()

                                -- Detect if path becomes blocked
                                blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
                                    -- Check if the obstacle is further down the path
                                    if blockedWaypointIndex >= nextWaypointIndex then
                                        -- Stop detecting path blockage until path is re-computed
                                        blockedConnection:Disconnect()
                                        -- Call function to re-compute new path
                                        followPath(destination)
                                    end
                                end)

                                -- Detect when movement to next waypoint is complete
                                if not reachedConnection then
                                    reachedConnection = Humanoid.MoveToFinished:Connect(function(reached)
                                        print("B")
                                        if reached and nextWaypointIndex < #waypoints then
                                            -- Increase waypoint index and move to next waypoint
                                            nextWaypointIndex += 1
                                            Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
                                        else
                                            reachedConnection:Disconnect()
                                            blockedConnection:Disconnect()
                                        end
                                    end)
                                end

                                -- Initially move to second waypoint (first waypoint is path start; skip it)
                                nextWaypointIndex = 2
                                print("A")
                                Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
                            else
                                warn("Path not computed!", success, path.Status, errorMessage)
                            end
                        end
                        followPath(Vector3.new(Seat.Position))

                        print("Yes")
                        Clicked:Disconnect()
                    end

The line warn("Path not computed!", success, path.Status, errorMessage) gives: Path not computed! true Enum.PathStatus.NoPath nil

1 answer

Log in to vote
0
Answered by 2 years ago

Fixed it. When I called Vector3.new around Seat.Position it was resetting the position to 0,0,0. So that was being returned, and it was impossible for the NPC to get to.

So to fix it I just removed vector3.new() at the line followPath(Vector3.new(Seat.Position)) so it became followPath(Seat.Position)

Ad

Answer this question