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

While true loop without yield?

How can I run a while true loop without a yield involved?

Edit: I want to run a while true loop infinitely, with code that still runs after the while true loop. I'm not having problems with exhausting the script.

7 comments

0

Do you mean while wait() do?

0

No, I mean while true do

0

Why would you do a while true loop when while wait loops are better?

0

I'm not familiar with them at all. While true sounds clearer to me.

0

while wait do is just while true do but with a wait like here "while wait(3) do end "

0

No, you cannot run it without yeild as the script will be exhausted

0

it will also lag ur pc to death

2 answers

✓ Accepted answer

If I understand correctly, you can't yield a loop until it's done. This is why you must run it in a different thread. There's two common ways of doing this, one being

spawn(function()

     -- code here

end)

And the other being

coroutine.wrap(function()

     -- code here

end)()

There are both slightly different but will run in different threads allowing you to run loops and continue to run code after it without yield from the loop. I prefer coroutine when doing something that must be exact because I think it runs immediately, not too sure though, other than that I use spawn.

Hope this helps :3

1 comment

0

Make sure to use the new task feature.

Always include a delay such as wait() inside an infinite loop, as omitting it can freeze the game. Please read the article about loops Here

4 comments

0

That isn't my problem. I probably didn't specify enough. I want to run a while true loop in a script, and continue running the while true loop, with code after the while true loop that runs instead of making another script.

0

use coroutine if you want to execute a code after an infinite loop https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

0

the loop will still run but in addition it can execute a code after the loop https://www.youtube.com/watch?v=f3bcZK7_F-E&t=325s

0

This is not the answer, this should br a comment

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