Advertisement
Guest User

Untitled

a guest
Mar 11th, 2021
38,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. local NPC = game.Workspace.Dummy
  2. local pathfinding_service = game:GetService("PathfindingService")
  3.  
  4. function getClosestPlayer()
  5.     local closest_player, closest_distance = nil, 200
  6.     for i, player in pairs(workspace:GetChildren()) do
  7.         if player:FindFirstChild("Humanoid") and player ~= NPC then
  8.             local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
  9.             if distance < closest_distance then
  10.                 closest_player = player
  11.                 closest_distance = distance
  12.             end
  13.         end
  14.     end
  15.     return closest_player, closest_distance
  16. end
  17.  
  18. while true do
  19.     local path = pathfinding_service:CreatePath()
  20.     local player, distance = getClosestPlayer()
  21.     if player and distance > 10 then
  22.         path:ComputeAsync(NPC.HumanoidRootPart.Position, player.PrimaryPart.Position)
  23.         local waypoints = path:GetWaypoints()
  24.         for _, waypoint in pairs(waypoints) do 
  25.             NPC.Humanoid:MoveTo(waypoint.Position)
  26.             while true do
  27.                 local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
  28.                 local distance_from_player = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
  29.                 if distance < 5 then
  30.                     break
  31.                 end
  32.                 if distance_from_player < 10 then
  33.                     NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
  34.                     break
  35.                 end
  36.                 wait()
  37.             end
  38.         end
  39.     end
  40.     wait()
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement