I have this ai script which I fixed earlier, it works technically and works the way I want it to but my problem
is that it works for one of the enemies in the folder and it works for a specific one. How would I fix it and make sure it works for all the enemies, heres the script
local pathfinding = game:GetService("PathfindingService")
for _, npc in ipairs(script.Parent:GetChildren()) do
if npc:IsA("Model") and npc:FindFirstChild("Zombie") then
local tank = npc
local humanoid = tank:FindFirstChild("Zombie")
local rightArm = tank:FindFirstChild("Right Arm")
local path = pathfinding:CreatePath()
local players = {}
local alerted = tank.Alerted.Value
local lastDamageTime = 0
humanoid.Health = health
humanoid.MaxHealth = health
humanoid.WalkSpeed = speed
while wait() do
local nearestPlayer = nil
local nearestDistance = radius
for _, child in pairs(workspace:GetChildren()) do
if child:IsA("Model") and child:FindFirstChild("Humanoid") then
local distance = (rightArm.Position - child.HumanoidRootPart.Position).magnitude
if distance < radius then
table.insert(players, child)
if distance < nearestDistance then
nearestPlayer = child
nearestDistance = distance
humanoid:MoveTo(nearestPlayer.HumanoidRootPart.Position)
end
end
end
end