Advertisement
gamesreboot

pathfind

Jul 12th, 2022 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. LoadScriptsEnv()
  2. local PFS = game:GetService("PathfindingService")
  3. local debris = game:GetService("Debris")
  4. local RunService = game:GetService("RunService")
  5.  
  6. local NPC = script.Parent
  7. local h = NPC.Humanoid
  8. local folder = NPC.Waypoints
  9.  
  10. local function findAndGoTo(destination)
  11.     local path = PFS:FindPathAsync(NPC.HumanoidRootPart.Position, destination.Position)
  12.     if path.Status == Enum.PathStatus.Success then
  13.         folder:ClearAllChildren()
  14.         for i,v  in ipairs(path:GetWaypoints()) do
  15.             local part = Instance.new("Part", folder)
  16.             part.Name = "Waypoint_" .. i
  17.             part.Size = Vector3.new(1,1,1)
  18.             part.Position = v.Position
  19.             part.Anchored = true
  20.             part.BrickColor = BrickColor.new("Really red")
  21.             part.Material = Enum.Material.Neon
  22.             part.CanCollide = false
  23.             part.Transparency = 1
  24.         end
  25.         for i,v  in ipairs(path:GetWaypoints()) do
  26.             h:MoveTo(v.Position)
  27.             h.MoveToFinished:Wait()
  28.             if v.Action == Enum.PathWaypointAction.Jump then
  29.                 h.JumpPower = 100 + math.abs(destination.Position.Y - NPC.HumanoidRootPart.Position.Y)
  30.                 h.Jump = true
  31.             end
  32.         end
  33.     else
  34.         h:MoveTo(destination.Position)
  35.         if destination.Position.Y - NPC.HumanoidRootPart.Position.Y then
  36.             if math.sqrt(math.pow((destination.Position.X-NPC.HumanoidRootPart.Position.X), 2) + math.pow((destination.Position.Z-NPC.HumanoidRootPart.Position.Z), 2)) <= 100 then
  37.                 h.JumpPower = 100 + math.abs(destination.Position.Y - NPC.HumanoidRootPart.Position.Y)
  38.                 h.Jump = true
  39.             end
  40.         end
  41.     end
  42. end
  43.  
  44.  
  45.  
  46. local function getNearest()
  47.     local nearestChar, nearestDist = nil, nil
  48.     for i, player in pairs(workspace:GetChildren()) do
  49.         if player:FindFirstChild("Humanoid") and not player:FindFirstChild("NPC") then
  50.             if player.Humanoid.Health ~= 0 then
  51.                 local distance = 0
  52.                 local thisPath = PFS:FindPathAsync(NPC.HumanoidRootPart.Position, player.HumanoidRootPart.Position)
  53.                 local prevPos
  54.                 if thisPath.Status == Enum.PathStatus.Success then
  55.                     for i,v  in ipairs(thisPath:GetWaypoints()) do
  56.                         if i ~= 1 then
  57.                             distance = distance + (prevPos - v.Position).magnitude
  58.                         end
  59.                         prevPos = v.Position
  60.                     end
  61.                 else
  62.                     distance = (NPC.HumanoidRootPart.Position - player.HumanoidRootPart.Position).magnitude
  63.                 end
  64.                 if nearestDist == nil then
  65.                     nearestChar = player
  66.                     nearestDist = distance
  67.                 else
  68.                     if distance < nearestDist then
  69.                         nearestChar = player
  70.                         nearestDist = distance
  71.  
  72.                     end
  73.                 end
  74.                 if distance <= 6 then
  75.                     if not nearestChar.HumanoidRootPart:FindFirstChild("BodyVelocity") then
  76.                         local bv1 = Instance.new("BodyForce", nearestChar.HumanoidRootPart)
  77.                         bv1.Force = (nearestChar.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit * math.random(0, 0.01) + Vector3.new(0, math.random(0, 0.01), 0)
  78.                         debris:AddItem(bv1, 0.2)
  79.                     end
  80.                    
  81.                     nearestChar.Humanoid.Health = 0
  82.                 end
  83.             end
  84.         end
  85.     end
  86.     return nearestChar
  87. end
  88.  
  89. local count = 0
  90. local po
  91. po = RunService.Heartbeat:Connect(function()
  92.     if not NPC:FindFirstChild("HumanoidRootPart") then po:Disconnect() script.Parent.Parent:Destroy() end
  93.     h.Health = h.MaxHealth
  94.     local nearestChar = getNearest()
  95.     if nearestChar ~= nil then
  96.         if nearestChar:FindFirstChild("HumanoidRootPart") then
  97.             local ray = Ray.new(NPC.HumanoidRootPart.Position, (nearestChar.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit * 10000)
  98.             local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
  99.             if hit then
  100.                 if hit:IsDescendantOf(nearestChar) then
  101.                     h:MoveTo(nearestChar.HumanoidRootPart.Position)
  102.                 else
  103.                     if count >= 100 then
  104.                         count = 0
  105.                         findAndGoTo(nearestChar.HumanoidRootPart)
  106.                     else
  107.                         count = count + 1
  108.                     end
  109.                 end
  110.             end
  111.         end
  112.     end
  113. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement