Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. local Behaviour = {}
  2. local RNG = Random.new()
  3.  
  4. function Behaviour:FindAllInRange(NPC,FindClosest,Range)
  5.     local InRange = {}
  6.    
  7.     for _,Turret in pairs(workspace.Turrets:GetChildren()) do
  8.         local Distance = (NPC.HumanoidRootPart.Position-Turret.PrimaryPart.Position).Magnitude
  9.         local Config = Turret:FindFirstChild("Configuration")
  10.         local Human = Turret:FindFirstChildOfClass("Humanoid")
  11.         local CurrentlyTargeting = NPC:FindFirstChild("CurrentlyTargeting")
  12.        
  13.         -- Note to self: add checks for health as well --
  14.         if Distance <= Range then
  15.             if Human and Config and CurrentlyTargeting then
  16.                 if Human.Health > 0 then
  17.                     if FindClosest then
  18.                         Range = Distance
  19.                         InRange = Turret
  20.                     else
  21.                         -- If the Zombie isn't currently targeting anything --
  22.                         if CurrentlyTargeting.Value == nil then
  23.                             table.insert(InRange, Turret)
  24.                         end
  25.                     end
  26.                 end
  27.             end
  28.         end
  29.     end
  30.     return InRange
  31. end
  32.  
  33. function Behaviour:GetTurretStates(NPC)
  34.     -- Checks the "Targeted" state of all turrets --
  35.     -- Returns false if not all Turrets are targeted --
  36.    
  37.     local Result = true
  38.     local TurretTable = Behaviour:FindAllInRange(NPC,false,1000)
  39.    
  40.     for _,Turret in pairs(TurretTable) do
  41.         local Config = Turret:FindFirstChild("Configuration")
  42.         local Human = Turret:FindFirstChildOfClass("Humanoid")
  43.        
  44.         if Config and Human then
  45.             if Config.Targeted.Value == false then
  46.                 Result = false
  47.             end
  48.         end
  49.     end
  50.    
  51.     return Result
  52. end
  53.  
  54. function Behaviour:FindTurret(NPC)
  55.     local AllTurretsAreTargeted = Behaviour:GetTurretStates(NPC)
  56.     local CurrentlyTargeting = NPC:FindFirstChild("CurrentlyTargeting")
  57.    
  58.     -- If every turret is targeted --
  59.     if AllTurretsAreTargeted and CurrentlyTargeting then
  60.         -- Get random turret from a table with all turrets in range of 30 studs --
  61.         local Turrets = Behaviour:FindAllInRange(NPC,false,30)
  62.         local ChosenTurret = Turrets[RNG:NextInteger(1,#Turrets)]
  63.         CurrentlyTargeting.Value = ChosenTurret
  64.         return ChosenTurret
  65.        
  66.     -- If not every turret is targeted --
  67.     elseif not AllTurretsAreTargeted then
  68.         -- If this runs, it'll just get the closest untargetted turret --
  69.         return Behaviour:FindAllInRange(NPC,true,30)
  70.     end
  71. end
  72.  
  73. return Behaviour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement