Introduction
Have you ever used while true do
before and it breaks your code or completely stops your Roblox Studio from functioning? Well, you have come to the right place! I have seen heaps of posts regarding while true do
on the Roblox DevForum and this tutorial will cover all the information you need to prevent this from happening ever again. So, lets begin!
What we will cover in this tutorial:
- How is this tutorial important to understand?
- What is
while true do
? - Why is
while true do
so bad and is there another way to use it? - What is
task.wait()
and is it better thanwait()
?
How is this tutorial important to understand?
This tutorial is highly recommended for beginners as it covers both good and bad practices when it comes to using while loops. And if you are an intermediate developer and you use while wait() do
, this tutorial will explain why that is bad practice too!
What is while true do
?
So to start off what is while true do
? Many of you probably already know what while true do
is but I am going to explain it to the beginner developers. while true do
is a loop that will repetitively run code inside of it instantaneously unless the condition is set to false. This is also known as a while loop:
while true do
print("Hello world!")
end
Now you may be thinking what could possible be wrong with this code and it looks fine. WRONG. When you test this out in Roblox Studio, it will either send you an error message in the output and break your code or crash Roblox Studio for you making it impossible to playtest your game.
Why is while true do
so bad and is there another way to use it?
Loads of Roblox developers have used while true do
once before in their code and that’s alright because we are all learners here and we all make mistakes which is what makes us human but its now time to stop using while true do
.
There have been many posts on the Roblox DevForum regarding while true do
and an example of a post may be: “Is it worth using while true do
?” or “Why does while true do
break my game?” and this my friend is the solution to all those posts.
Since while true do
is an infinitely running loop which runs code inside it instantaneously, it causes the script to become exhausted therefore breaking your code and crashing your Roblox Studio. Most of the time you may have seen an error message sent to the output saying: “Script timeout: exhausted allowed execution time”.
This basically says the script timed out and we need to add a delay (in seconds) inside of it so the script doesn’t get exhausted and timeout again. So, lucky for us we can still use the while loop but we just need to insert a delay to it so that it doesn’t break the game or crash Roblox Studio.
It is highly recommended that you put a 0.8 - 1 second delay (or higher) into the while loop so the script doesn’t timeout:
while task.wait(1) do
print("Hello world!")
end
OR
while true do
print("Hello world!")
task.wait(1)
end
But wait a second, what is task.wait(1)
? Why not use wait(1)
?
Lets explore that right now!
What is task.wait()
and is it better than wait()
?
"
task.wait()
yields the current thread until the given duration (in seconds) has elapsed and then resumes the thread on the next Heartbeat step." - @WallsAreForClimbing
In simple terms, task.wait()
is an improved version of wait()
.
local myElapsedTime = task.wait(2) --> Wait for 2 seconds
print(myElapsedTime) --> My result: 2.0139438999999584
How is this helpful did you say? Well, since while true do
needs a delay in it so the script doesn’t get exhausted and timeout, task.wait()
is the perfect solution to this!
Final result:
while task.wait(1) do
print("Hello world!")
end
OR
while true do
print("Hello world!")
task.wait(1)
end
Conclusion
Congratulations! You have now just made while true do
functional for your game!
I hope this tutorial was helpful to many developers out there who are just discovering this dilemma. If you have any questions please PM me.
Helpful Resources:
- Avoiding wait() and why - Better explanation on task.wait()