Guest User

Coding Help

a guest
Nov 24th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. debugMode = false
  2. targetNPCs = false
  3.  
  4. --
  5.  
  6. h = script.Parent.Parent:WaitForChild("Humanoid")
  7. pathService = game:GetService("PathfindingService")
  8. targetV = script.Parent:WaitForChild("Target")
  9.  
  10. function closestTargetAndPath()
  11.     local humanoids = {}
  12.     if targetNPCs then
  13.         local function recurse(o)
  14.             for _,obj in pairs(o:GetChildren()) do
  15.                 if obj:IsA("Model") then
  16.                     if obj:findFirstChild("Humanoid") and obj:findFirstChild("Torso") and obj.Humanoid ~= h and obj.Humanoid.Health > 0 and not obj:findFirstChild("ForceField") then
  17.                         table.insert(humanoids,obj.Humanoid)
  18.                     end
  19.                 end
  20.                 recurse(obj)
  21.             end
  22.         end
  23.         recurse(workspace)
  24.     else
  25.         for _,v in pairs(game.Players:GetPlayers()) do
  26.             if v.Character and v.Character:findFirstChild("HumanoidRootPart") and v.Character:findFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 and not v:findFirstChild("ForceField") then
  27.                 table.insert(humanoids,v.Character.Humanoid)
  28.             end
  29.         end
  30.     end
  31.     local closest,path,dist
  32.     for _,humanoid in pairs(humanoids) do
  33.         local myPath = pathService:ComputeRawPathAsync(h.Torso.Position,humanoid.Torso.Position,500)
  34.         if myPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
  35.             -- Now that we have a successful path, we need to figure out how far we need to actually travel to reach this point.
  36.             local myDist = 0
  37.             local previous = h.Torso.Position
  38.             for _,point in pairs(myPath:GetPointCoordinates()) do
  39.                 myDist = myDist + (point-previous).magnitude
  40.                 previous = point
  41.             end
  42.             if not dist or myDist < dist then -- if true, this is the closest path so far.
  43.                 closest = humanoid
  44.                 path = myPath
  45.                 dist = myDist
  46.             end
  47.         end
  48.     end
  49.     return closest,path
  50. end
  51.  
  52. function goToPos(loc)
  53.     h:MoveTo(loc)
  54.     local distance = (loc-h.Torso.Position).magnitude
  55.     local start = tick()
  56.     while distance > 4 do
  57.         if tick()-start > distance/h.WalkSpeed then -- Something may have gone wrong. Just break.
  58.             break
  59.         end
  60.         distance = (loc-h.Torso.Position).magnitude
  61.         wait()
  62.     end
  63. end
  64.  
  65. while wait() do
  66.     local target,path = closestTargetAndPath()
  67.     local didBreak = false
  68.     local targetStart
  69.     if target and h.Torso then
  70.         targetV.Value = target
  71.         targetStart = target.Torso.Position
  72.         roaming = false
  73.         local previous = h.Torso.Position
  74.         local points = path:GetPointCoordinates()
  75.         local s = #points > 1 and 2 or 1
  76.         for i = s,#points do
  77.             local point = points[i]
  78.             if didBreak then
  79.                 break
  80.             end
  81.             if target and target.Torso and target.Health > 0 then
  82.                 if (target.Torso.Position-targetStart).magnitude < 1.5 then
  83.                     local pos = previous:lerp(point,.5)
  84.                     local moveDir = ((pos - h.Torso.Position).unit * 2)
  85.                     goToPos(previous:lerp(point,.5))
  86.                     previous = point
  87.                 end
  88.             else
  89.                 didBreak = true
  90.                 break
  91.             end
  92.         end
  93.     else
  94.         targetV.Value = nil
  95.     end
  96.     if not didBreak and targetStart then
  97.         goToPos(targetStart)
  98.     end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment