Advertisement
SpacecowboyHX

[Polished] Roblox advanced AI Pathfinding script

Mar 14th, 2020
8,081
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. --SETTINGS
  2. local showpath = false
  3. local Destination = workspace.Destination-- YOU CAN CHANGE THIS.
  4. --SCRIPT CORE
  5. if Destination==nil then
  6.     error("There is no Destination set for the bot to follow, please check line 3.")
  7. end
  8. local destination = Destination.Position
  9. local pathfindingService = game:GetService("PathfindingService")
  10. local humanoid = script.Parent.Humanoid
  11. humanoid.WalkSpeed = 16
  12. local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
  13. local path = pathfindingService:CreatePath()
  14. path:ComputeAsync(body.Position, destination)
  15. local waypoints = path:GetWaypoints()
  16. repeat wait(1) humanoid:MoveTo(Destination.Position) path:ComputeAsync(body.Position, destination) until path.Status == Enum.PathStatus.Success
  17. local waypoints = path:GetWaypoints()
  18. for i = 1,#waypoints do
  19.     if showpath == true then
  20.         local part = Instance.new("Part",workspace)
  21.         part.Name = i
  22.         part.Anchored = true
  23.         part.Position = waypoints[i].Position
  24.         part.CanCollide = false
  25.         part.Size = Vector3.new(1.5,1.5,1.5)
  26.         part.Transparency = 1
  27.         if i ~=1 then
  28.             other = workspace:FindFirstChild(tostring(i-1))
  29.             if other~=nil then
  30.                 local mag = (other.Position-part.Position).Magnitude
  31.                 local trail = Instance.new("Part",workspace)
  32.                 trail.Name = "trail"..i
  33.                 trail.Size = Vector3.new(0.5,0.5,mag)
  34.                 trail.CFrame = CFrame.new(other.Position,part.Position)*CFrame.new(0,0,-mag/2)
  35.                 trail.Anchored = true
  36.                 trail.CanCollide = false
  37.             end
  38.         end
  39.     end
  40. end
  41. for k, waypoint in pairs(waypoints) do
  42.     if path.Status == Enum.PathStatus.Success then
  43.         humanoid.WalkToPoint = waypoint.Position
  44.         if waypoint.Action == Enum.PathWaypointAction.Jump then
  45.             humanoid.WalkSpeed = 0
  46.             wait(0.3)
  47.             humanoid.WalkSpeed = 16
  48.             humanoid.Jump = true
  49.         end
  50.         humanoid.MoveToFinished:Wait(2)
  51.     else
  52.         repeat path:ComputeAsync(body.Position,destination) until path.Status == Enum.PathStatus.Success
  53.     end
  54. end
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement