Advertisement
Guest User

Roblox Pathfinding Zombie

a guest
Oct 27th, 2019
32,867
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.65 KB | None | 1 0
  1. local myHuman = script.Parent:WaitForChild("Humanoid")
  2. local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
  3. local head = script.Parent:WaitForChild("Head")
  4. local lowerTorso = script.Parent:WaitForChild("LowerTorso")
  5.  
  6. local grab = script.Parent:WaitForChild("Grab")
  7. local grabAnim = myHuman:LoadAnimation(grab)
  8. grabAnim.Priority = Enum.AnimationPriority.Action
  9.  
  10. local grabSound = head:WaitForChild("Attack")
  11. local screamSound = head:WaitForChild("Scream")
  12.  
  13. local clone = script.Parent:Clone()
  14.  
  15. function walkRandomly()
  16.     local xRand = math.random(-50,50)
  17.     local zRand = math.random(-50,50)
  18.     local goal = myRoot.Position + Vector3.new(xRand,0,zRand)
  19.    
  20.     local path = game:GetService("PathfindingService"):CreatePath()
  21.     path:ComputeAsync(myRoot.Position, goal)
  22.     local waypoints = path:GetWaypoints()
  23.    
  24.     if path.Status == Enum.PathStatus.Success then
  25.         for _, waypoint in ipairs(waypoints) do
  26.             if waypoint.Action == Enum.PathWaypointAction.Jump then
  27.                 myHuman.Jump = true
  28.             end
  29.             myHuman:MoveTo(waypoint.Position)
  30.             local timeOut = myHuman.MoveToFinished:Wait(1)
  31.             if not timeOut then
  32.                 print("Got stuck")
  33.                 myHuman.Jump = true
  34.                 walkRandomly()
  35.             end
  36.         end
  37.     else
  38.         print("Path failed")
  39.         wait(1)
  40.         walkRandomly()
  41.     end
  42. end
  43.  
  44. function findPath(target)
  45.     local path = game:GetService("PathfindingService"):CreatePath()
  46.     path:ComputeAsync(myRoot.Position,target.Position)
  47.     local waypoints = path:GetWaypoints()
  48.    
  49.     if path.Status == Enum.PathStatus.Success then
  50.         for _, waypoint in ipairs(waypoints) do
  51.             if waypoint.Action == Enum.PathWaypointAction.Jump then
  52.                 myHuman.Jump = true
  53.             end
  54.             myHuman:MoveTo(waypoint.Position)
  55.             local timeOut = myHuman.MoveToFinished:Wait(1)
  56.             if not timeOut then
  57.                 myHuman.Jump = true
  58.                 print("Path too long!")
  59.                 findPath(target)
  60.                 break
  61.             end
  62.             if checkSight(target) then
  63.                 repeat
  64.                     print("Moving directly to the target")
  65.                     myHuman:MoveTo(target.Position)
  66.                     attack(target)
  67.                     wait(0.1)
  68.                     if target == nil then
  69.                         break
  70.                     elseif target.Parent == nil then
  71.                         break
  72.                     end
  73.                 until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
  74.                 break
  75.             end
  76.             if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
  77.                 print("Target has moved, generating new path")
  78.                 findPath(target)
  79.                 break
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. function checkSight(target)
  86.     local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40)
  87.     local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
  88.     if hit then
  89.         if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 3 then
  90.             print("I can see the target")
  91.             return true
  92.         end
  93.     end
  94.     return false
  95. end
  96.  
  97. function findTarget()
  98.     local dist = 50
  99.     local target = nil
  100.     local potentialTargets = {}
  101.     local seeTargets = {}
  102.     for i,v in ipairs(workspace:GetChildren()) do
  103.         local human = v:FindFirstChild("Humanoid")
  104.         local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
  105.         if human and torso and v.Name ~= script.Parent.Name then
  106.             if (myRoot.Position - torso.Position).magnitude < dist and human.Health > 0 then
  107.                 table.insert(potentialTargets,torso)
  108.             end
  109.         end
  110.     end
  111.     if #potentialTargets > 0 then
  112.         for i,v in ipairs(potentialTargets) do
  113.             if checkSight(v) then
  114.                 table.insert(seeTargets, v)
  115.             elseif #seeTargets == 0 and (myRoot.Position - v.Position).magnitude < dist then
  116.                 target = v
  117.                 dist = (myRoot.Position - v.Position).magnitude
  118.             end
  119.         end
  120.     end
  121.     if #seeTargets > 0 then
  122.         dist = 200
  123.         for i,v in ipairs(seeTargets) do
  124.             if (myRoot.Position - v.Position).magnitude < dist then
  125.                 target = v
  126.                 dist = (myRoot.Position - v.Position).magnitude
  127.             end
  128.         end
  129.     end
  130.     if target then
  131.         if math.random(20) == 1 then
  132.             screamSound:Play()
  133.         end
  134.     end
  135.     return target
  136. end
  137.  
  138. function attack(target)
  139.     if (myRoot.Position - target.Position).magnitude < 5 then
  140.         grabAnim:Play()
  141.         grabSound:Play()
  142.         if target.Parent ~= nil then
  143.             target.Parent.Humanoid:TakeDamage(25)
  144.         end
  145.         wait(0.4)
  146.     end
  147. end
  148.  
  149. function died()
  150.     wait(5)
  151.     clone.Parent = workspace
  152.     game:GetService("Debris"):AddItem(script.Parent,0.1)
  153. end
  154.  
  155. myHuman.Died:Connect(died)
  156.  
  157. lowerTorso.Touched:Connect(function(obj)
  158.     if not obj.Parent:FindFirstChild("Humanoid") then
  159.         myHuman.Jump = true
  160.     end
  161. end)
  162.  
  163. function main()
  164.     local target = findTarget()
  165.     if target then
  166.         myHuman.WalkSpeed = 16
  167.         findPath(target)
  168.     else
  169.         myHuman.WalkSpeed = 8
  170.         walkRandomly()
  171.     end
  172. end
  173.  
  174. while wait(0.1) do
  175.     if myHuman.Health < 1 then
  176.         break
  177.     end
  178.     main()
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement