Hey guys, as in the title. You know, you can do
1 | while true do |
2 | --Script here |
3 | wait( 10 ) |
4 | end |
but that will stop the whole script, or in other words, it would get stuck in there and repeat into an infinity AND BEYOND! But I need it to be looping infinitely "in the background" so the script can continue its way down but the infinite loop will be there. I hope you get it :D
I did some research, but didn't really help - I found something with WaitForAll, but I want to understand my scripts. Also, try to explain a bit (sentence above).
Thanks in advance!
you can use spawn()
or coroutines.
01 | spawn( function () |
02 | while true do |
03 | print ( "john" ) |
04 | wait( 1 ) |
05 | end |
06 | end ) |
07 |
08 | local foo = coroutine.wrap( function () |
09 | while true do |
10 | print ( "hello john!" ) |
11 | wait( 1 ) |
12 | end |
13 | end ) |
14 |
15 | foo() |
You can use the spawn function, which runs the function in a separate thread, like it's running in a different script.
1 | spawn( function () |
2 | while true do |
3 | --script here |
4 | wait( 10 ) |
5 | end |
6 | end ) |
7 |
8 | print ( 'hi' ) |
You can also use coroutines https://developer.roblox.com/articles/Beginners-Guide-to-Coroutines https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Roblox#spawn