gamesreboot

ai

Jul 16th, 2022 (edited)
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | None | 0 0
  1. LoadScriptsEnviroment()
  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 t
  9.  
  10. local function findAndGoTo(destination)
  11.         if not NPC:FindFirstChild("HumanoidRootPart") then
  12.            t:Disconnect()
  13.             NPC.Parent:Destroy()
  14.         end
  15.     local path = PFS:FindPathAsync(NPC.HumanoidRootPart.Position, destination.Position)
  16.     if path.Status == Enum.PathStatus.Success then
  17.  
  18.  
  19.         for i,v  in ipairs(path:GetWaypoints()) do
  20.             h:MoveTo(v.Position)
  21.             h.MoveToFinished:Wait()
  22.             if v.Action == Enum.PathWaypointAction.Jump then
  23.                 --h.JumpPower = 100 + math.abs(destination.Position.Y - NPC.HumanoidRootPart.Position.Y)
  24.                 h.Jump = true
  25.             end
  26.         end
  27.     else
  28.         h:MoveTo(destination.Position)
  29.         if destination.Position.Y - NPC.HumanoidRootPart.Position.Y then
  30.             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
  31.                 --h.JumpPower = 100 + math.abs(destination.Position.Y - NPC.HumanoidRootPart.Position.Y)
  32.                 h.Jump = true
  33.             end
  34.         end
  35.     end
  36. end
  37.  
  38.  
  39.  
  40. local function getNearest()
  41.         if not NPC:FindFirstChild("HumanoidRootPart") then
  42.            t:Disconnect()
  43.             NPC.Parent:Destroy()
  44.         end
  45.     local nearestChar, nearestDist = nil, nil
  46.     for i, player in pairs(workspace:GetChildren()) do
  47.         if player:FindFirstChild("Humanoid") and player ~= script.Parent and not player:FindFirstChild("NPC") then
  48.             if player.Humanoid.Health ~= 0 then
  49.                 local distance = 0
  50.                 local thisPath = PFS:FindPathAsync(NPC.HumanoidRootPart.Position, player.HumanoidRootPart.Position)
  51.                 local prevPos
  52.                 if thisPath.Status == Enum.PathStatus.Success then
  53.                     for i,v  in ipairs(thisPath:GetWaypoints()) do
  54.                         if i ~= 1 then
  55.                             distance = distance + (prevPos - v.Position).magnitude
  56.                         end
  57.                         prevPos = v.Position
  58.                     end
  59.                 else
  60.                     distance = (NPC.HumanoidRootPart.Position - player.HumanoidRootPart.Position).magnitude
  61.                 end
  62.                 if nearestDist == nil then
  63.                     nearestChar = player
  64.                     nearestDist = distance
  65.                 else
  66.                     if distance < nearestDist then
  67.                         nearestChar = player
  68.                         nearestDist = distance
  69.  
  70.                     end
  71.                 end
  72.                 if distance <= 4.5 then
  73.                     if not nearestChar.HumanoidRootPart:FindFirstChild("BodyVelocity") then
  74.                         local bv1 = Instance.new("BodyForce", nearestChar.HumanoidRootPart)
  75.                         bv1.Force = (nearestChar.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit * 20000 + Vector3.new(0, 5000, 0)
  76.                     end
  77.  
  78.                     nearestChar.Humanoid.Health = 0
  79.  
  80.                     --Use this is you have Ragdoll after death installed (you'll have to change some things in code here)
  81.                     local ragdoll = workspace:WaitForChild(nearestChar.Name .. "-2")
  82.                     if not ragdoll.HumanoidRootPart:FindFirstChild("BodyVelocity") then
  83.                         local bv2 = Instance.new("BodyForce", ragdoll:FindFirstChild("HumanoidRootPart"))
  84.                         bv2.Force = (ragdoll.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit * 20000 + Vector3.new(0, 5000, 0)
  85.                         debris:AddItem(bv2, 1)
  86.                     end
  87.                 end
  88.             end
  89.         end
  90.     end
  91.     return nearestChar
  92. end
  93.  
  94. local count = 0
  95.  
  96. t = RunService.Heartbeat:Connect(function()
  97.         if not NPC:FindFirstChild("HumanoidRootPart") then
  98.            t:Disconnect()
  99.             NPC.Parent:Destroy()
  100.         end
  101.     h.Health = h.MaxHealth
  102.     local nearestChar = getNearest()
  103.     if nearestChar ~= nil then
  104.         if nearestChar:FindFirstChild("HumanoidRootPart") then
  105.             local ray = Ray.new(NPC.HumanoidRootPart.Position, (nearestChar.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit * 1000000000)
  106.             local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
  107.             if hit then
  108.                 if hit:IsDescendantOf(nearestChar) then
  109.                     h:MoveTo(nearestChar.HumanoidRootPart.Position)
  110.                 else
  111.                     if count >= 100 then
  112.                         count = 0
  113.                         findAndGoTo(nearestChar.HumanoidRootPart)
  114.                     else
  115.                         count = count + 1
  116.                     end
  117.                 end
  118.             end
  119.         end
  120.     end
  121.     task.wait()
  122. end)
  123. local b
  124. b = h.Died:Connect(function()
  125.     t:Disconnect()
  126.     b:Disconnect()
  127. end)
  128.  
Add Comment
Please, Sign In to add comment