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

How to use the PathFindingService dynamically?

Asked by 5 years ago
Edited 5 years ago

So I have a procedurally generated path, but I want to make sure that it is solvable by the PathFindingService before I put the player at the SpawnPoint, to do this, I've added the following, using the older FindPathAsync() method. (I tried the newer CreatePath() approach that is on the wiki, but Studio gives me an error saying: PathfindingService.CreatePath() is a new API and has not been fully deployed yet. I don't think it's working properly, though I'm not getting any bugs at run-time. My issue is that it is Async and yields until returning a path, but if there is not a path available, then there might be an infinite loop? Here's the snippet

        local solvable = false

        while not solvable do
            local container = game.Workspace:FindFirstChild("MazeContainer")
            if container then  -- Get rid of the old path if it exists
                container:Destroy()
                container = nil
            end

            BuildMaze(FromDimensions(_sizeX, _sizeZ, _seed, _posY), _sizeX, _sizeZ, _posY, _buildX, _buildZ)

            local path = pathfinding:FindPathAsync(startPoint, goalPoint)
            wait (3) -- Give the async operation time to complete if it can
            local wp = path:GetWaypoints()
            if wp == nil or #wp < 2 then 
                _seed = _seed + 1       -- This seed wasn't solvable, move to the next iteration
            else
                solvable = true
            end     

        end

I'd prefer to yield here (at the wait(3) line) until a result is found or not found, i.e. Enum.PathStatus.NoPath any tips on getting Studio to let me use the newer API?

Answer this question