Avoid using while true do & while wait() do!

Apparently, wait() doesn’t have much of a difference from task.wait() so it’s gonna throw an timeout error at you. You have to use task.wait(0), and because the accuracy is spot-on for this function, it’ll work gracefully.

Not sure when using loops was a bad thing? Just because they can crash if not used properly is not a good reason.

1 Like

I don’t really see the point of using

while true do
	task.wait(1)
end

since all it does is run in an infinite loop that executes about every 1 second. However, a better use for that is this:

task.spawn(function()
	local start
	while true do
		start = os.clock()
		for _, item in pairs(some table) do
			-- Do some work.
		end
		task.wait(1 - (os.clock() - start))
	end
end)

That’s one pattern that I’ve used it for. Why would I use something like this? To perform background processing at timed intervals. I’ve used this on both client and server code. Works quite well. For the OP to state that people should avoid using while true do just because one can easily screw things up with it is utter nonsense and demonstrates that the OP does not understand software development constructs or why certain patterns are used.

The reason why

while true do
	print("Hello World")
end

appears to crash Roblox is that there is no opportunity for the task scheduler to gain control which is why Roblox will freeze. Unless you’re using Actors for Parallel LUA, Roblox LUA script execution is serial between threads. If the current thread does not yield (which is what wait() and task.wait() is for), then the other threads that are waiting to run can’t and the system locks up. Roblox LUA has a script timer so if the run time exceeds that, the engine throws an exception and breaks the LUA execution. So in other words wait() and task.wait() are used in what is known as a cooperative multithreading system to yield the current thread.

Here’s another:

local loopflag = true
while loopflag == true do
	loopflag = false
	for _, item in pairs(some table) do
		if <some condition> then
			-- Do some work.
			loopflag = true
		elseif <some other condition> then
			-- Do some other work.
			loopflag = true
		else
			-- Exit the for loop
			break
		end
	end
end

The above construct is what I use if the predicate for the while condition is too complex to express in one line, or there are a complex set of conditions that must be met for the loop to exit or continue.

In software development, there are three basic constructs of code:

  • Sequence: Instructions/Statements are executed one right after the other.
  • Condition: The instruction sequence that is executed is based on some condition; branching.
  • Loop: A set of instructions are to be executed none, one, or more than one time.

These constructs are needed for a language to be Turing complete.

As for the difference between wait() and task.wait(), the difference is simple:

  • wait() waits for about 1/30th (0.033) second before resuming the thread execution. This is one frame.
  • task.wait() waits for about 1/60th (0.016) second before resuming the thread execution. This is two frames. However, in reality, it’s closer to 0.0202 to be honest.

Nothing wrong with using wait() if you only need a 1/30 second delay as opposed to a 1/60 delay.

Of course, it all comes down to this adage that I’m sure everyone has heard before:

The right tool for the job.