What code takes priority inside of a "repeat-until" loop?

Hello, I am currently working on making a turn-based combat game, and I was wondering about repeat-until loops.

Currently, the loop runs until either all the enemies or the heroes die, ending the battle. However, in between all of that, each character (and enemy once I script it) will take turns and have unlimited time to take their turn.


This is why there is another repeat-until loop within this one. I was just wondering if the main repeat-until loop will take priority and end when it’s supposed to or if it gives priority to the loop inside of it. I hope that makes sense, and if it doesn’t I can try to explain further. Thanks for any help!

2 Likes

Code like this runs sequentially. That means the outer loop won’t continue until the inner loop finishes.

As for the outer loop ending at the intended time: the outer loop will not end the moment the outer loop’s end condition is met. In addition, it might not end at all if the end condition becomes true while the TurnTaken == true is being waited for but then the end condition becomes false again. (ie if the outer loop is busy waiting on the inner loop it won’t see that the end condition was met.)

The thing above isn’t a problem if the spawned heros and spawned mobs aren’t changed outside the loop.


You can replace your wait loop with something that might be more clear like this:

-- If TurnTaken isn't already true wait for it to change
if hero:GetAttribute("TurnTaken") == false then
    hero:GetAttributeChangedSignal("TurnTaken"):Wait()
end
1 Like

We lagging the server with this one :speaking_head:

Joke aside please don’t do a loop inside a loop it is a performance nightmare
if you want to have a loop run until the attribute changed use

Object:GetAttributeChangedSignal("Attribute"):Connect(function()
	-- your code here
end)
2 Likes

Because they’re using task.wait(1) and not creating multiple loops within the outer one (they’re all sequential) performance shouldn’t be a problem.

2 Likes

Thank you, this is very helpful. I’ll try to be aware of those other issues you mentioned as well to make sure I don’t run into problems later.

1 Like

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