Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Repeating a function, what's the best way to do it, with minimal lag etc?

I currently only know of two ways, a third I'm unfamiliar with which I'm not sure would work. What's the most efficient way of repeating something over again, for example let's say you have a repeating game that has to call on a function/s that set up the game for people play. Is there another way that I've yet to find, (likely).

One:

while true do
--code
wait(--[[whatever time is set]])
end

Second:

--Currently using this below, over the while true do ... end
function a()
wait(1)
a()
end

Third: I'm not very familiar with these but coroutine's are these a viable way of setting a loop or something that must be called again and again?

3 comments

0

Try repeat and until

0

Oh, I knew that one too, I was just wondering, is there another way besides the ones I mentioned, I'd prefer to not use those because I feel they are inefficient and cause more lag.

0

Lag isn't caused by the loop -- it's what you put inside the loop. If you can get away with a higher wait time than wait(0), that will also help.

1 answer

✓ Accepted answer

The second method is called recursion. Recursion shouldn't be used for simply repeating yourself.

Recursion has some overhead. The overhead is reduced if you use tail calls


There is no "lag" associated with any kind of iteration.

The while loop is the most readable and typical. Use that.

This discussion is preserved as it was. New answers, comments, and votes are closed.