Advertisement
ScriptreMooCow

Bugfixed pathfinding2.1

May 23rd, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. --[[
  2. This script demonstrates simple pathfinding.
  3. https://developer.roblox.com/articles/Pathfinding
  4. --]]
  5.  
  6. local PathfindingService = game:GetService("PathfindingService")
  7.  
  8. print("Variables for the zombie, its humanoid, and destination")
  9. local zombie = game.Workspace.Zombie
  10. local humanoid = zombie.Humanoid
  11. local points = game.Workspace.Points
  12.  
  13. --Variables to store waypoints table and zombie's current waypoint
  14. local waypoints
  15. local currentWaypointIndex
  16.  
  17. local function followPath(destinationObject)
  18. if destinationObject == nil then return end -- base case, thanks Dr. Burks for CSC 212!
  19. local path = PathfindingService:CreatePath()
  20. print("Create the path object")
  21.  
  22. print("Compute and check the path")
  23. path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.Position)
  24.  
  25. print("Empty waypoints table after each new path computation")
  26. waypoints = {}
  27.  
  28. --recursive function
  29. local function onPathBlocked(blockedWaypointIndex, point)
  30. print("Check if the obstacle is further down the path")
  31. if blockedWaypointIndex > currentWaypointIndex then
  32. -- Call function to re-compute the path
  33. followPath(point)
  34. end
  35. end
  36.  
  37. local function onWaypointReached(reached)
  38. if reached and currentWaypointIndex < #waypoints then
  39. currentWaypointIndex = currentWaypointIndex + 1
  40. humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
  41. end
  42. end
  43.  
  44. print("Connect 'Blocked' event to the 'onPathBlocked' function")
  45. path.Blocked:Connect(onPathBlocked)
  46. print("Connect 'MoveToFinished' event to the 'onWaypointReached' function")
  47. humanoid.MoveToFinished:Connect(onWaypointReached)
  48.  
  49. if path.Status == Enum.PathStatus.Success then
  50. print("Get the path waypoints and start zombie walking")
  51. waypoints = path:GetWaypoints()
  52. for _, waypoint in pairs(waypoints) do
  53. local part = Instance.new("Part")
  54. part.Shape = "Ball"
  55. part.Material = "Neon"
  56. part.Size = Vector3.new(0.6, 0.6, 0.6)
  57. part.Position = waypoint.Position
  58. part.Anchored = true
  59. part.CanCollide = false
  60. part.Parent = game.Workspace
  61. end
  62. print("Move to first waypoint")
  63. currentWaypointIndex = 1
  64. humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
  65. else
  66. print("Error (path not found); stop humanoid")
  67. humanoid:MoveTo(zombie.HumanoidRootPart.Position)
  68. end
  69.  
  70. return true
  71. end
  72.  
  73. local waitime = 10
  74.  
  75. followPath(points.pointA)
  76. wait(waitime)
  77. followPath(points.pointB)
  78. wait(waitime)
  79. followPath(points.pointC)
  80. wait(waitime)
  81. followPath(points.pointD)
  82. wait(waitime)
  83. followPath(points.pointA)
  84. wait(waitime)
  85. followPath(points.pointC)
  86. wait(waitime)
  87. followPath(points.pointB)
  88. wait(waitime)
  89. followPath(points.pointD)
  90. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement