SHARE
TWEET

AI_Path

a guest Dec 11th, 2019 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local zombie = script.Parent.Parent
  2. local CollectionService = game:GetService("CollectionService")
  3. local PathfindingService = game:GetService("PathfindingService")
  4. local TweenService = game:GetService("TweenService")
  5.  
  6. local PatrolNodes = CollectionService:GetTagged("ZombiePatrolNode")
  7. local possiblePatrolNodes = {}
  8.  
  9. for _, node in pairs(PatrolNodes)do
  10.     if node:IsDescendantOf(workspace)then
  11.         table.insert(possiblePatrolNodes, node)
  12.     end
  13. end
  14.  
  15. local DRAWDEBUG = {}
  16.  
  17. local function run()
  18.     if zombie.Brain.Patrolling.Value == false and zombie.Brain.LockedOn.Value == nil and zombie.Brain.ServerControl.Value == true then
  19.         zombie.Brain.Patrolling.Value = true
  20.         local path = nil
  21.         repeat
  22.             wait()
  23.             local NodeSel = possiblePatrolNodes[math.random(1, #possiblePatrolNodes)]
  24.             path = PathfindingService:CreatePath({AgentRadius = 5, AgentHeight = 5, AgentCanJump = false})
  25.            
  26.             path:ComputeAsync(zombie.HumanoidRootPart.Position, NodeSel.Position)
  27.            
  28.             if path.Status == Enum.PathStatus.Success then
  29.                 break
  30.             else
  31.                 zombie.Humanoid.Jump = true
  32.                 zombie.Humanoid:Move(Vector3.new(zombie.HumanoidRootPart.Position.X + math.random(-1,1), zombie.HumanoidRootPart.Position.Y, zombie.HumanoidRootPart.Position.Z + math.random(-1,1)))
  33.             end
  34.         until 1 + 1 == 3
  35.         local PatrolPath = path:GetWaypoints()
  36.        
  37.         for _, waypoint in pairs(PatrolPath)do
  38.             if zombie.Brain.ServerControl.Value == false then return end
  39.             zombie.Humanoid:MoveTo(waypoint.Position)
  40.             repeat wait() until (zombie.HumanoidRootPart.Position - waypoint.Position).Magnitude <= 10
  41.            
  42.         end
  43.         zombie.Brain.Patrolling.Value = false
  44.        
  45.         for _, obj in pairs(DRAWDEBUG)do
  46.             obj:Destroy()
  47.         end
  48.         DRAWDEBUG = {}
  49.     end
  50. end
  51. if script.Parent.Parent:IsDescendantOf(game.Workspace.Zombies) then run(); wait(1); zombie.Brain.Patrolling.Value = false; end
  52. zombie.Brain.Patrolling.Changed:Connect(function(val) if val == false then run() end end)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top