Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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?

0
Try repeat and until iDev_Percy 45 — 8y
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. Evadable 65 — 8y
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. Perci1 4988 — 8y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

Ad

Answer this question