SHARE
TWEET

Untitled

a guest Oct 19th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local PathfindingService = game:GetService("PathfindingService")
  2. local ready = true
  3.  
  4. local NPC = script.Parent
  5. local humanoid = script.Parent.Humanoid
  6.  
  7. local x = 0
  8.  
  9. while true do
  10.     wait()
  11.     if ready == true then
  12.         ready = false
  13.        
  14.         local blockedConnection
  15.         local finishedConnection
  16.        
  17.         repeat
  18.             wait()
  19.             destination = game.Workspace.Points:GetChildren()[math.random(1,#game.Workspace.Points:GetChildren())]
  20.         until(script.Parent.HumanoidRootPart.Position - destination.Position).magnitude > 17
  21.        
  22.         -- Create the path object
  23.         local path = PathfindingService:CreatePath({
  24.             AgentCanJump = false
  25.         })
  26.        
  27.         -- Variables to store waypoints table and NPC's current waypoint
  28.         local waypoints = {}
  29.         local currentWaypointIndex = 1
  30.        
  31.         local function followPath(destinationObject)
  32.             -- Compute and check the path
  33.             path:ComputeAsync(NPC.HumanoidRootPart.Position, destinationObject.Position)
  34.             -- Empty waypoints table after each new path computation
  35.             waypoints = {}
  36.            
  37.             if path.Status == Enum.PathStatus.Success then
  38.                 -- Get the path waypoints and start NPC walking
  39.                 waypoints = path:GetWaypoints()
  40.                 -- Move to first waypoint
  41.                 currentWaypointIndex = 1
  42.                 humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
  43.             else
  44.                 -- Error (path not found); stop humanoid
  45.                 humanoid:MoveTo(NPC.HumanoidRootPart.Position)
  46.             end
  47.         end
  48.        
  49.         local function onWaypointReached(reached)
  50.             if reached and currentWaypointIndex < #waypoints then
  51.                 currentWaypointIndex = currentWaypointIndex + 1
  52.                 humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
  53.                 --If NPC reaches destination
  54.                 if (script.Parent.HumanoidRootPart.Position - destination.Position).magnitude < 6 then
  55.                     ready = true
  56.                     finishedConnection:Disconnect()
  57.                     blockedConnection:Disconnect()
  58.                 end
  59.             end
  60.         end
  61.        
  62.         local function onPathBlocked(blockedWaypointIndex)
  63.             -- Check if the obstacle is further down the path
  64.             if blockedWaypointIndex > currentWaypointIndex then
  65.                 -- Call function to re-compute the path
  66.                 followPath(destination)
  67.             end
  68.         end
  69.        
  70.         followPath(destination)
  71.        
  72.         -- Connect 'Blocked' event to the 'onPathBlocked' function
  73.         blockedConnection = path.Blocked:Connect(function(...)
  74.             onPathBlocked(...)
  75.         end)
  76.        
  77.         -- Connect 'MoveToFinished' event to the 'onWaypointReached' function
  78.         finishedConnection = humanoid.MoveToFinished:Connect(function(...)
  79.             onWaypointReached(...)
  80.         end)
  81.        
  82.     end
  83. 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