I have a question, I am a newbie scripter but here is my script:
while true do
end
while true do
end
my question is, will all my while true do loops run at the same time?
I have a question, I am a newbie scripter but here is my script:
while true do
end
while true do
end
my question is, will all my while true do loops run at the same time?
No, every loop runs sequentially.
You have to use coroutine
in order to run several pseudo-threads simultaneously.
The 2nd loop won’t run until the first one stops, and since the first one will never stop, the 2nd one won’t run at all.
If you want both of them to run simultaneously, you need to use one of these 3 functions: spawn
, coroutine.wrap
, coroutine.create (with coroutine.resume)
If you’re a newbie, I recommend you start with spawn
, and then once you gain more knowledge look into the differences between those 3.
You use spawn
like this:
spawn(function()
--stuff here
end)
You can also pass a specific function to it:
local function foo()
print("yay i was called")
end
spawn(foo)
Now, the way you’d use it in your example code is the following:
spawn(function()
while true do
end
end)
while true do
end
Notice how you have to apply it to the first loop, which is pretty logical since otherwise no code beneath it would run, including the spawn
.
I now refer you to
Use
coroutine.wrap(
function()
end
)()
instead.
use coroutines, since OOP exists, that’ll yield indefinitely until its told not to, also you might want to add a wait() somewhere in there… otherwise it’s going to lag a lot!
I now refer you to
spawn
is much more beginner-friendly, since it doesn’t return a function that you have to call (less confusion for beginners), and its behaviour is easier to understand in general: it never propagates errors, unlike coroutine.wrap which only does before the first yield, it never returns the function’s return, unlike coroutine.wrap which does if it doesn’t yield before returning, and so on.
I am aware of the “downsides” of spawn, however when it comes to learning the basics there is no need to be pedantic. Hence why I mentioned that the OP should look into the other 2 functions after they gain more knowledge.
As someone once told me with using ValueObjects over dictionaries for certain work, I think it’s significantly more important to learn about the dangers of using spawn early on so you can avoid bad habits and wrong technical knowledge later when you improve your craft.
There’s no better a time than starting out as a beginner to learn that spawn is bad and should not be used with its reliance on legacy pipelines and no guarantees. Convenience is not an excuse to start out using something bad and there are adequate alternatives that can be used, from FastSpawn to Promises and even coroutines alone. The right and best methods should be taught so that knowledge sticks for when the developer’s knowledge expands and they’re more capable of understanding what it is they’re doing, why they do it and why it’s better than alternatives.
Coroutines are surprisingly not complicated. I was once on that boat but it became significantly easier to understand the concept behind them when I learned to treat the term as “co-routine” instead of things it wasn’t. A routine, prefixed with co-, meaning it happens alongside something else. From there, you just need a bit of help stitching them together and you can learn about their technical details later.
I’m not saying OP would do this, but when you tell someone to use spawn and they end up actually needing to use it in many places (which is probably abusing spawn rather than using it properly), they end up learning the hard way that spawn doesn’t have any guarantees of running its code - a top team, the developers behind Rolantis, learned this as well and switched all their spawns out.
Spawn is convenient but bad and you avoid picking bad, whether you know if its downsides or not and it’s not pedantic to tell someone not to use it. Use better alternatives. Start learning strong and smart, not learning bad practice and then trouble yourself later down the line with going back and fixing everything because of a mistake that could’ve been rectified earlier. Promise is a great alternative which even allows you to handle what should be done in case of error.
I think a better question that I’d like to ask, all this discussion aside, is knowing what this is for. Why are you, OP, interested in knowing if both run at the same time or not? I get it if you’re just asking for the sake of having technical knowledge on hand but surely you might have a reason why you’re asking this question, such as having run into this case while coding? If your use case is known, better alternatives for loops might be able to be provided here, such as switching to an event-driven system.
Again, I didn’t say the OP should stick to using spawn
for the rest of their life. I merely believe starting out with it is much more beginner-friendly in terms of understanding how it works, and this is actually a common approach used when learning any subject or skill: starting from the easiest stuff even if it isn’t the best. Kids learn to crawl before they start to walk.
Not to mention the OP’s code is most likely used to just learn. Meanwhile the impression I’m getting from your post is that you think the OP is in the middle of scripting a medium game and needs help with deciding whether to use spawn or not. This is most likely not the case.
If you’ve ever learned maths at school (and I’m assuming you have), you should know that most of the time you operate on ideal secarios, “dummy” ones so to say, due to the fact that it’s only for the purpose of you learning and understanding the logic behind it. The case is the same here. The OP asked how to solve his hypothetical problem operating on his dummy code, as they’ve most likely never done this before. And the worst thing you can do in such scenario is give them a complex solution if there’s a simpler one at hand. (This is also an answer to your 2nd reply).
Furthermore, even roblox wiki presents the article about spawn() as a kind of prequel to the one about coroutines (and states spawn’s “downsides” only after explaining how it works)