Are loops really that scary?

People have always made loops sound like a horrible thing to use. The scum on the bottom soap, the gum under the desk. But they are a fundamental part of any Roblox game, right?

I get that it is all about whats inside and how you use them that determines performance impacts, but it seems like everyone looks down upon all loops in general, at least in my experience.

As I have increased my coding knowledge, loops have become the scooby doo villain of the coding world. Not as bad as I first thought.

But I have been stuck for a long time trying to figure out better alternatives for loops only because I thought they weren’t good to use. I was completely stumped on game security and anti exploit.

“You can’t send a remote to trigger a server side check, that doesn’t make any sense. And I can’t just have a loop constantly checking all the time…”

Apparently you can. I didn’t know it was common practice, and I assumed it wasn’t because of this bad rap.

9 Likes

No.

Expensive loops are something to look into. If you have a loop that runs a lot of lines in quick succession that can render the game slow, find a different way to run it using functions and events.

If the loop is somehow necessary, consider slowing down the loop by adding wait(). Although you should be considering utilizing events over loops.

4 Likes

Loops are a perfectly normal practise. for loops can save a lot of code, and a lot of effort (especially if you don’t know what, or how much, of a table you’ll have)

Here are two of the most common problems when it comes to loops:

Repeat

In general, you should avoid using repeat unless you absolutely need to.
Avoiding repeat wait() until is better covered by @Kampfkarren here.

I can’t think on-the-spot of any good use cases of repeat, but I am absolutely certain they do exist.

While

You should never use while true do unless absolutely necessary

If you need to check something on a regular basis, you can use RunService to accomplish this behaviour, including running timed tasks.

It is acceptable to have one main game loop (e.g. in a round game), but they should be avoided at all costs.

Other uses of while

These are usually covered in Kampfkarren’s post (above). But another example I’m going to use is a lightbar.

local sirenOn = false

while sirenOn do
 lightBar.Red = true
 wait(1)
 lightBar.Red = false
 lightBar.Blue = true
 wait(1)
 lightBar.Blue = false
 wait(1)
end

In cases like this (where you can’t use an event as it’s a timed task that needs to occur over and over) it’s acceptable to have a loop. But of course you should use potentially infinite loops lightly

Crashing

Loops can crash your game if they’re running forever (or in something that’s going to run infinitely anyway, e.g. for i = 1, math.huge

This can lag really badly or crash, which is one of the main arguments against using loops.

Additionally

@Seranok (Developer Product Manager) made a post on why you can avoid unnecessary yields with coroutines: (Only visible to New Members or full members)
https://devforum.roblox.com/t/psa-use-coroutines-to-avoid-unecessary-waiting/28338

13 Likes

Loops are not scary and often it’s just an irrational condemning of them. What’s a problem is how you use those loops and whether it’s necessary or not for a given use case. Event-driven systems are often more effective and preferred than loops. Some developers tend to use loops to solve a lot of problems unnecessarily and the misuse of loops is part of where this stigma against loops comes from.

6 Likes

Repeat compiles similarly to a while loop (in terms of efficiency at least), and there isn’t really any issue with using it compared to a while loop in appropriate situations - however a repeat statement will always trigger once, whilst a while loop will only trigger if the condition is met.

3 Likes

There is nothing wrong with using repeat or while true do loops appropriately. The repeat form is just a variation of while for when you know you need the loop to always run at least once. It’s functionally equivalent to a while true do with an if condition then break end as its last inner statement.

You should not use while true do to make something like a game main event loop, because code erroring inside the loop can interrupt it and it will not self resume. RunService heartbeat is what you want driving your game loop. But as a loop construct for running logic, while true do is fine, provided there is code inside that is guaranteed to hit a break or return statement or yield in a very short, deterministic amount of time. A while loop that can execute for seconds or more without yielding is bad code and a bug, not a flaw of the loop structure itself.

5 Likes

Absolutely not.

As mentioned above a lot of bad rep comes from the inappropriate usage of of loops. It’s not commonly thought of but using loops in specific more complex situations can actually save you tons of memory opposed to doing it without. You’ll only pain yourself and kill off projects by avoiding them, they’re there for a reason.

1 Like