Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Behaviour = {}
- local RNG = Random.new()
- function Behaviour:FindAllInRange(NPC,FindClosest,Range)
- local InRange = {}
- for _,Turret in pairs(workspace.Turrets:GetChildren()) do
- local Distance = (NPC.HumanoidRootPart.Position-Turret.PrimaryPart.Position).Magnitude
- local Config = Turret:FindFirstChild("Configuration")
- local Human = Turret:FindFirstChildOfClass("Humanoid")
- local CurrentlyTargeting = NPC:FindFirstChild("CurrentlyTargeting")
- -- Note to self: add checks for health as well --
- if Distance <= Range then
- if Human and Config and CurrentlyTargeting then
- if Human.Health > 0 then
- if FindClosest then
- Range = Distance
- InRange = Turret
- else
- -- If the Zombie isn't currently targeting anything --
- if CurrentlyTargeting.Value == nil then
- table.insert(InRange, Turret)
- end
- end
- end
- end
- end
- end
- return InRange
- end
- function Behaviour:GetTurretStates(NPC)
- -- Checks the "Targeted" state of all turrets --
- -- Returns false if not all Turrets are targeted --
- local Result = true
- local TurretTable = Behaviour:FindAllInRange(NPC,false,1000)
- for _,Turret in pairs(TurretTable) do
- local Config = Turret:FindFirstChild("Configuration")
- local Human = Turret:FindFirstChildOfClass("Humanoid")
- if Config and Human then
- if Config.Targeted.Value == false then
- Result = false
- end
- end
- end
- return Result
- end
- function Behaviour:FindTurret(NPC)
- local AllTurretsAreTargeted = Behaviour:GetTurretStates(NPC)
- local CurrentlyTargeting = NPC:FindFirstChild("CurrentlyTargeting")
- -- If every turret is targeted --
- if AllTurretsAreTargeted and CurrentlyTargeting then
- -- Get random turret from a table with all turrets in range of 30 studs --
- local Turrets = Behaviour:FindAllInRange(NPC,false,30)
- local ChosenTurret = Turrets[RNG:NextInteger(1,#Turrets)]
- CurrentlyTargeting.Value = ChosenTurret
- return ChosenTurret
- -- If not every turret is targeted --
- elseif not AllTurretsAreTargeted then
- -- If this runs, it'll just get the closest untargetted turret --
- return Behaviour:FindAllInRange(NPC,true,30)
- end
- end
- return Behaviour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement