while true do > while wait() do


  • I was testing the difference between while true do and while wait() do, and I got some very interesting results:

    true as a condition: 12.917328596115
    wait() as a condition: 12.950897693634

    I waited 12 seconds because I'm slow at copy pasting

    Almost a whole 0.4 second difference, I'd say that's a pretty significant change.

    local start = tick()
    
    spawn(function()
        while true do
            wait(1)
            print("true as a condition:", tick() - start)
        end
    end)
    
    local new = tick()
    
    spawn(function()
        while wait(1) do
            print("wait() as a condition:", tick() - new)
        end
    end)
    

  • Because it's possibly 'more accurate', doesn't mean that it's better to use

    It's mostly preference, I like to use wait() more because you cannot forget a wait() then, and it gets less messy.

    Also, unless you're making a timer or something like that, a 0.04 difference is way too small for people to notice

  • Global Moderator

    @RedcommanderV2 Wrong. Using wait() as the condition of a while loop makes absolutely no sense.

    Use

    while true do
        -- ...
        wait(t)
    end
    

    It's clearer.

    Also, @incapaz, that's not how you run benchmarks at all. For good results you would instead run the test a few thousand times and get the average time taken. Minus the amount time actually waited. The problem is, using wait() as the condition of the loop means we can't retrieve the time waited, so to keep the results consistent we'll refrain from subtracting it from both tests. However what we could do is subtract the requested wait time from the result, but I'll leave it up to you to try that out if you so wish.

    local iterations = 10000
    local wait_time = 0.4
    
    ----------
    -- using `true` as condition
    local t1 = tick()
    
    for i = 1, iterations do
        while true do
            -- ...
            wait(wait_time)
        end
    end
    
    t1 = tick() - t1
    t1 = t1/iterations
    
    
    ----------
    -- using `wait(t)` as condition
    local t2 = tick()
    
    for i = 1, iterations do
        while wait(wait_time) do
            -- ...
        end
    end
    
    t2 = tick() - t2
    t2 = t2/iterations
    
    
    ----------
    -- end results:
    print(string.format("average time taken for solution #1, `while true do... end`: %.2f", t1))
    print(string.format("average time taken for solution #2, `while wait(t) do ... end`: %.2f", t2))
    

    will give better results.


  • Since when is preference wrong, I haven't ever had a problem with wait()

    It's better than craching your studio and having to rewrite your code because you lost it.. the chances of that happening with using while wait() do are way smaller

  • Global Moderator

    @RedcommanderV2 Preferences can be wrong. Because you like something doesn't mean it's not wrong. Having preferences is not wrong, but preferring while wait() do ... end to while true do ... end is.

    while true do ... end is "better to use", as it is clearer. It clearly indicates that the loop will run infinitely, as its condition will always be true.

    How does using wait() as the loop's condition tell you that? The answer is it doesn't.
    How does using wait() as a loop's condition even make any sense? It doesn't either.

    Using wait() as the condition for a while loop is really a hack (not "hack" as in gaining unauthorized access to a system, but in the sense that it is a trick and an inelegant solution). The only reason it works is that the function returns a number corresponding the actual time Roblox waited before resuming the thread and, in Lua, in a condition context, any non-nil or false value is considered as true (in programming terms, we say everything but nil and false is truthy).

    It makes absolutely no sense to use that number as the loop's condition.

    Furthermore, the chances of forgetting to add a call to wait() and crashing studio are not smaller in any way if you follow good habits and write wait() immediately when creating your while block.

    If forgetting to add a call to wait() is really your concern, you could it at the top of your loop, just after the condition test:

    while true do
        wait(t)
        -- do something...
    end
    

    Besides, studio has an autosave feature which makes sure to save everything every few seconds, as well as when launching a Play test. You shouldn't lose anything but your last few seconds of work.


  • I never said it's better, I just said while true do, isn't necessarily better. I do agree that it can be considered as a 'hack' but it works, and it doesn't use any kind of deprecated code or so...

    As for the chances of forgetting a 'wait', if you scripted for some time, you won't be likely to forget it, but for new scripters, it's something you can easily forget or just not know about, that's for me a reason to use it, so new scripters will hopefully also use it (or use while true do with a wait), also, it's less code.

    Autosave... some people just like to turn it off, as they don't want an autosave every 5 minutes or so, also, a craching studio is not something that you want to happen...

  • Global Moderator

    @RedcommanderV2
    while true do ... end definitely is the better option. It makes sense, the result is functionally the same and, if you ever venture outside of Roblox, you will definitely encounter this construct everywhere.

    Trial and error is how you learn. After forgetting it a few times you'll learn to make sure not to forget it. Teaching newbies to write bad code that they don't even fully understand -- and without telling them why or how it works -- for the sake of having them not commit occasional little mistakes is stupid. We don't want newbies to learn bad habits, we want them to learn the right way to do things. Teach them to write good code or don't teach them at all.

    The "also it's less code" argument is a bad argument, and deprecation is not the only reason not to use a feature or write code in some way or another. You should first write code not for the machines executing it, but for the people reading and editing that code in the future. Write clear and easily readable code. It doesn't matter if it's a bit longer, what's important is that it is clear and understandable by everyone. Source code is for people, not for machines. Don't write code that "just works", write code that both works and is self-explanatory.

    Finally, if people turn autosave off, that's their problem. They shouldn't turn it off in the first place if they don't want to lose their progress.


  • Link is entirely correct. I would take his advice if I were you.


  • @Link150 yeah sorry for the bad testing. But still, I got a pretty big result. And @RedcommanderV2, in pure Lua, the wait() function does not exist, it is a Roblox function. So if you ever see some script in pure Lua with a while loop, do not expect a wait function.

  • Global Moderator

    @incapaz Your test results are worthless, though. Code doesn't always run at the same speed. It's possible you just got lucky. Even with several samples, it's often too volatile to measure. That's why you take the average time taken over several iterations.


  • This post is deleted!

  • @incapaz Too much work. Don't bother.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.