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.
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.
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
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
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.
use coroutine if you want to execute a code after an infinite loop https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine
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
This is not the answer, this should br a comment
This discussion is preserved as it was. New answers, comments, and votes are closed.
7 comments
Do you mean while wait() do?
No, I mean while true do
Why would you do a while true loop when while wait loops are better?
I'm not familiar with them at all. While true sounds clearer to me.
while wait do is just while true do but with a wait like here "while wait(3) do end "
No, you cannot run it without yeild as the script will be exhausted
it will also lag ur pc to death