Hi, i’m trying to call a function for each tower in my towers folder, but if I have the FindTarget() function, the loop stops and the script only applies to one tower.
With the function:
Intended: without the function()
As you can see it runs three times for all the three towers, but when I have the function in the script it only applies it to one tower.
for i, tower in ipairs(TowersFolder:GetChildren()) do
print("1 child")
if tower:FindFirstChild("Humanoid") then
if TowersFolder:FindFirstChild(tower.Name) then
local TowerInfo = tower:FindFirstChild("TowerInfo")
local Active = TowerInfo:FindFirstChild("Active")
if Active.Value == false then
Active.Value = true
FindTarget()
end
end
end
end
If the code after the FindTarget() call doesn’t continue right away, that’s because FindTarget never returns. It’s probably yielding, with a wait call in an infinite loop or something similar.
You can get around this in two ways:
Change FindTarget so it doesn’t yield
Create a new thread for FindTarget so it can be multitasked along with the calling script
I can’t tell you exactly how to change FindTarget since you didn’t post it. You can call it in a new thread using task.spawn.