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

While true loop without yield?

Asked by 3 years ago
Edited 3 years ago

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.

0
Do you mean while wait() do? Rumln 0 — 3y
0
No, I mean while true do CoopGaming7 20 — 3y
0
Why would you do a while true loop when while wait loops are better? Rumln 0 — 3y
0
I'm not familiar with them at all. While true sounds clearer to me. CoopGaming7 20 — 3y
View all comments (3 more)
0
while wait do is just while true do but with a wait like here "while wait(3) do end " Rumln 0 — 3y
0
No, you cannot run it without yeild as the script will be exhausted sne_123456 439 — 3y
0
it will also lag ur pc to death sne_123456 439 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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

0
Make sure to use the new task feature. h8run 30 — 3y
Ad
Log in to vote
-1
Answered by 3 years ago

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

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. CoopGaming7 20 — 3y
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 gamingwithjbthepro 76 — 3y
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 gamingwithjbthepro 76 — 3y
0
This is not the answer, this should br a comment AlexanderYar 788 — 3y

Answer this question