SHARE
TWEET

Untitled

a guest Dec 15th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. while true do
  2.   wait()
  3.  
  4.   local PathfindingService = game:GetService("PathfindingService")
  5.  
  6.   local character = script.Parent
  7.   local humanoid = character.Humanoid
  8.   local root = humanoid.RootPart -- humanoid.RootPart is faster than character.HumanoidRootPart
  9.  
  10.   local destination = workspace.Destination2.Position -- use workspace, not game.Workspace
  11.  
  12.   local path = PathfindingService:CreatePath()
  13.  
  14.   local waypoints
  15.   local currentWaypointIndex
  16.  
  17.   local function followPath(destinationobject)
  18.     -- computer and check the path
  19.     path:ComputeAsync(root.humanoid.RootPart.Position, destinationobject.PrimaryPart.Position)
  20.  
  21.     -- Empty waypoints table after each new computation
  22.     waypoints = {}
  23.  
  24.     if path.Status == Enum.PathStatus.Success then -- We check if the path was successfull here
  25.  
  26.       local waypoints = path:GetWaypoints()
  27.  
  28.       for i = 1, #waypoints do -- Iterate through every waypoint
  29.         local waypoint = waypoints[i]
  30.  
  31.         if waypoint.Action == Enum.PathWaypointAction.Walk then
  32.             humanoid:MoveTo(waypoint.Position)
  33.             humanoid.MoveToFinished:Wait()
  34.         else
  35.             humanoid.Jump = true
  36.         end
  37.     end
  38.   -- End missing here?
  39.  
  40.     local function onWaypointReached(reached)
  41.         if reached and currentWaypointIndex < #waypoints then
  42.             currentWaypointIndex = currentWaypointIndex + 1
  43.  
  44.             humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
  45.         end
  46.     end
  47.  
  48.     local function onPathBlocked(blockedWaypointIndex)
  49.       -- Check if the obstacle is further down the path
  50.       if blockedWaypointIndex > currentWaypointIndex then
  51.         -- Call function to re-compute the path
  52.         followPath(destination)
  53.        
  54.         -- Connect 'Blocked' event to the 'onPathBlocked' function
  55.         path.Blocked:Connect(onPathBlocked)
  56.  
  57.         -- Connect 'MoveToFinished' event to the 'onWaypointReached' function
  58.         humanoid.MoveToFinished:Connect(onWaypointReached)
  59.  
  60.         followPath(destination)
  61.       end
  62.     end    
  63.   end
  64. end
  65. end -- Extra end?
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top