it6318

Custom AI Script

Nov 28th, 2020
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local pathfindingservice = game:GetService("PathfindingService")
  2.  
  3. local db = false
  4.  
  5. local torso = nil
  6. local torsochar = nil
  7.  
  8. local zombie = script.Parent:WaitForChild("Zombie")
  9. local me = script.Parent:WaitForChild("Head")
  10. script.Parent.PrimaryPart:SetNetworkOwner(nil)
  11. local target = nil
  12. local aim
  13. local path
  14. local a = 1
  15. local movetotimeout = 0
  16. local movementdone = false
  17.  
  18. local chooserdb = false
  19.  
  20.  
  21.  
  22. function FindNearest(position)
  23.     local lowest = math.huge -- infinity
  24.     local NearestPlayer = nil
  25.     for i,v in pairs(game.Players:GetPlayers())do
  26.         if v and v.Character then
  27.             local distance = v:DistanceFromCharacter(position)
  28.             if distance < lowest then
  29.                 lowest = distance
  30.                 NearestPlayer = v
  31.             end
  32.         end
  33.     end
  34.     return NearestPlayer
  35. end
  36.  
  37. function targetposition()
  38.     torso = FindNearest(me.Position)
  39.     if torso ~= nil then
  40.         torsochar = game.Workspace:FindFirstChild(torso.Name)
  41. --      print("torsochar"..tostring(torsochar.HumanoidRootPart.Position))
  42.         target = torsochar.HumanoidRootPart.Position
  43.     end
  44.     return target
  45. end
  46.  
  47. -- if target is within a radius and raycasting shows that there is nothing in the way,
  48. -- use MoveTo (on loop) but if ray casting shows something in the way then use pathfinder.
  49.  
  50.  
  51. function movementchooser()
  52.     if chooserdb == false then
  53.         chooserdb = true
  54.         local barrierfinder = Ray.new(me.Position, (targetposition() - me.Position).Unit * 500)
  55.         local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(barrierfinder, script.Parent:GetChildren())
  56.         if hit == torsochar:WaitForChild("RayHitbox") then
  57.             print("MoveTo selected")
  58.                 chooserdb = false
  59.             return "MoveTo"
  60.         else
  61.             print("PathFind Selected")
  62.                 chooserdb = false
  63.             return "Pathfind"
  64.         end
  65.     end
  66. end
  67.  
  68.  
  69. function Pathfinding()
  70.     aim = targetposition()
  71.     path = pathfindingservice:CreatePath()
  72.     path:ComputeAsync(me.Position, aim)
  73.     local waypoints = path:GetWaypoints()
  74.     for i, waypoint in pairs(waypoints)do
  75.         if waypoint.Action == Enum.PathWaypointAction.Jump then
  76.             zombie:ChangeState(Enum.HumanoidStateType.Jumping)
  77.         end
  78.         zombie:MoveTo(waypoint.Position)
  79.         zombie.MoveToFinished:Wait()
  80.     end
  81.     return "done"  
  82. end
  83.  
  84.  
  85. function MoveTo()
  86.     print("MoveTo Activated")
  87.     movementdone = false
  88.     movetotimeout = 0
  89.     zombie:MoveTo(targetposition())
  90.     while movetotimeout < 1 do
  91.         wait(0.1)
  92.         movetotimeout = movetotimeout + 0.1
  93.         print("MoveTo Check", movetotimeout)
  94.     end
  95.     if movetotimeout >= 1 then
  96.         print("timed out")
  97.         movementchooser()
  98.         if movementchooser() == "Pathfind" then
  99.             Pathfinding()
  100.             repeat
  101.                 wait()
  102.             until Pathfinding() == "done"
  103.         end
  104.     else
  105.         if zombie.MoveToFinished == true then
  106.             print("MoveTo Finished")
  107.             movementdone = true
  108.             return "Done"
  109.         end    
  110.     end
  111.         movementdone = true
  112.         return "Done"
  113. end
  114.  
  115.  
  116.  
  117. wait(15)
  118. while true do
  119.     MoveTo()
  120.     repeat
  121.         wait()
  122.     until movementdone == true
  123.     wait()
  124. end
Advertisement
Add Comment
Please, Sign In to add comment