Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Pathfinding brick does go into a different direction unless it is done with the first location?

I am learning pathfinding and I tried to make a brick(brick1) find the fastest way to get to another brick(brick2) and then start moving there and that works fine but if I move brick2 to another location with my cursor then brick1 has to finish going to where brick2 was before but I want it to just instantly go to where brick2 is.

while true do
    path = game:GetService("PathfindingService"):ComputeRawPathAsync(game.Workspace.Start.Position, game.Workspace.End.Position, 200)
    points = path:GetPointCoordinates()

    for p=1,#points do
		game.Workspace.Start.Position = points[p]
		wait(.5)
    end
    wait(.01)
end           

1 answer

The reason that it is going through the original path is because you have a for loop that runs until the path is complete. If the path changes, the script doesn't know about it until the for loop is complete.

Also, you should use variables in your script to prevent things like getting the PathfindingService thousands of times per second. Instead, create a variable before the while loop and reference that instead.

This discussion is preserved as it was. New answers, comments, and votes are closed.