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

[SOLVED] How to make function repeat every 10 seconds without stopping the whole script?

Asked by 6 years ago
Edited 4 years ago

Hey guys, as in the title. You know, you can do

while true do
  --Script here
    wait(10)
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!

0
If you any have one infinite loop I would just put it at the bottom of your script. User#5423 17 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

you can use spawn() or coroutines.

spawn(function()
    while true do
        print("john")
        wait(1)
    end
end)

local foo = coroutine.wrap(function()
    while true do
        print("hello john!")
        wait(1)
    end
end)

foo()
0
I will user the Spawn() ! Thanks mate! Doge_Brigade 94 — 6y
0
I guess I can use spawn(MyFunction), right? Doge_Brigade 94 — 6y
Ad
Log in to vote
1
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You can use the spawn function, which runs the function in a separate thread, like it's running in a different script.

spawn(function()
   while true do
       --script here
       wait(10)
   end
end)

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

Answer this question