Trying to figure out how to make i, v in

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:
image

Intended: without the function()
image

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
1 Like

try using pairs instead of ipairs. maybe that’s the problem

1 Like

Tried that before, still doesn’t work as intended

1 Like

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.

1 Like

Thanks for the help, using task.spawn I was able to solve my issue.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.