Why do people say not to use Wait() in a while block? (and more)


  • READ THIS POST WITH THIS SONG PLAYING.
    (if you are a true memer you should recognize the first note immediately)]

    I really don't see the difference here.

    Most people have the argument of "Don't use while wait() do, becuase it doesn't properly emphasize what you're trying to get across."

    But how so? The first time I came across the use of while wait() do, I immediately read it as "While you're waiting for blank, do this," but at first, that was a wrong notion.

    Later in my scripting days, I ended up reading it as "After you wait for blank, do this."

    I feel like it serves the same purpose as "while true do," because when I also came across the use of "while * do", I read it as "When this condition is true, do this." But, aren't you also waiting for the condition to be true?

    But, one could also say:

    Wait() is a function, and is not nil. If you were to plug the wait() function into a condition, it will run through it, because again, it is not nil. But, for some reason, it will also execute it's assigned function.

    I'd love your input on this. Reading people's opinions is a fun pastime.

    But, here comes the second question.

    If you were to define a function, then put it into a conditional statement, it will return as "not nil", but also "nil?"
    Alongside that, if the function were to return "true", then the "not" usage of the function will be falsey. You could probably guess for the contrary.

    Let's put it into code terms.

    Lets say I had my completely stupid function that I came up with in less than 5 minutes with a pad and pencil.

    Looks like you weren't able to load the content. Try again?

    Cool. Now, let's go and make some tests. I made six conditional statements that has to do with the same function.

    For the first two statements, I made the function return false.

    Wow, your internet must be bad, huh.

    Let's see what it returns.

    How can it not display some flashy colors?! Christ.

    This should make sense. The first one returns false, and that means the condition is falsey. Therefore, the code block did not run. The second one checks if it is returning false or nil, and as it did, the code block will run.

    Now, let us run our true statements, shown below.

    Frickin heck is wrong with your internet? You might wanna restart it.

    Again, let us see what it returns.

    I honestly have no idea what to say anymore.

    Again, some normal stuff. At this point, I might just be trying to extend the length of the forum post without knowing. Anyways, as expected, the first code block is true, because true == true, and second is falsey because false!==true.

    Now, here comes the weird part to me, at least. For this test, we will be providing no variables for the function to use, again, shown below.

    what

    Now, lets see what kind of behemoth this thing pulled out from the deep, dark depths of kingdom come.

    000.saveme

    Uh, excuse me, what? I'm a stupid person. It took me QUITE a while to figure out why this happens, and unsurprisingly, it is a simple answer.

    Apparently, by default, a function returns nil. The function itself is not nil, so that's good to know. I found this out from the second conditional.

    But, alas, you likely didn't notice this that the first statement did not show. Instead, it called the function!
    Wait, what?
    It called it twice..?
    oh.
    odd.

    Anyways. Why did it call twice? I have no idea. Oh wait, I do. Maybe because the function was called in the first statement, and was called again in the second statement, working with two code blocks? I wonder, if I were to put a wait() inside of that if statement, would it yield the if block? Let's test.

    It's jif time!

    And, what do you know? The function is called, AND the if statement isn't false! But.. wait..
    Isn't a function supposed to return nil by default?

    So, does that mean that if I were to have a function that returned true after 5, and called it in an if statement, it would wait(5) and complete the if statement? Odd. Don't think it did that in the previous examples.

    Or.. did it..?

    Kidding. I was just leading you on with the wait thing. If you were to call the wait function inside of a print, it returns two values. The time provided, and the.. total execution time?
    I have no idea what the second number is. Anyways, it's just two numbers with abstract purpose, in my opinion.
    Also, there was no second question. Got ya again!

    I hope ya enjoyed reading this, and I hope you also enjoyed being led on. Go out there and make your lives extraordinary!

    "Carpe diem. Seize the day boys. Make your lives extraordinary" ~Robin Williams


  • @Fifkee

    Hey Vsauce! Michael here.


  • Using wait(t) as your condition is a hack. It abuses the fact that wait returns a number according to the time Roblox yielded the thread for. When you say while wait(10) do you're really just saying while 10.00348 do or however long the thread was yielded for. The number isn't exact, but very close. It makes zero sense to use that number returned as the loop's condition.

    Shorter Code

    One argument for using wait(t) it is that "it's shorter". Yes, shorter code may be desired, but not to the point where it ruins readability.

    while true do "crashes your game"

    "while true do crashes your game" is also another argument. This is false, if you have a wait in there somewhere.

    hah won't be long until people start using
    repeat ... until not wait(t)


  • @incapaz said in Why do people say not to use Wait() in a while block? (and more):

    Using wait(t) as your condition is a hack.

    while wait() do
    

    Wow I hacked the government


  • @incapaz Stop..

    Please stop with saying how people should script and giving opinions as the reason why!

    Opinions are a nice thing, but shouldn't be used to prove your point, that's wasting time.

    "Using wait(t) as your condition is a hack. It abuses the fact that wait returns a number according to the time Roblox yielded the thread for. When you say while wait(10) do you're really just saying while 10.00348 do or however long the thread was yielded for. The number isn't exact, but very close. It makes zero sense to use that number returned as the loop's condition."

    Lots of things are wrong with this..
    "while 10.00348 do"
    First you're assuming that we're talking about English here, that's not true, we're talking about Lua, while lua can be considered "English", if you would throw in a command like "Give me cookies" this will absolutely not work

    So it doesn't make sense in English, but hey, it's Lua, not english after all, and as everything besides false and nil is considered "true", it is basicly the same as while true do + a wait, just a shortcut!

    "SHORTER CODE
    One argument for using wait(t) it is that "it's shorter". Yes, shorter code may be desired, but not to the point where it ruins readability."

    ^^ OPINION WARNING ^^

    I've never heard anyone say to me "while wait() do" ruins readability in code, it actually improves it a lot, you immidially know that it will run every x seconds, you don't have to go and search for that line of code you putted in somewhere randomly.

    "WHILE TRUE DO "CRASHES YOUR GAME""

    It does :p, unless you put a wait() in it, but hey, that's not even neccessary for while wait() do!

    NOTE

    • while wait() do is not bad to use and will certaintly not ruin your game code, it's not deprecated (why would it ever lol) and for sure won't ever be, considering it as a "hack" and saying it isn't logical is preference and opinion, I don't share the same thoughts as why that wouldn't be logical (it's not english!)

    • people already made clear that they don't think that it is bad to use, repeating it won't change their minds

    Can we now stop please?


  • ^^

    Same as @RedcommanderV2


  • while wait(5) do
    print'Hey VSauce, Michael here!'
    end
    

    is equal to

    while true do
    wait(5)
    print'Hey VSauce, Michael here!'
    end
    

    Just because one person stated their opinion doesn't mean it's globally agreed upon. If so, it becaomes a fact.

    Also, just so you know.
    I use

    function b()
    print'Hey VSauce, Michael Here!'
    end;
    function a()
    wait(5)
    
    b()
    
    end;
    

    That loop is more superior than any of yours combined. Don't @ me.
    kek

    oh yeah plus i'm friends with like a lot of big coder guys that can code literally everything at the snap of a finger and they use while wait() do like they uhhhh
    can someone give me a simile that relates to frequency

  • Global Moderator

    @Aimarekin Not that way, smartass.

  • Global Moderator

    People are so stubborn, smh.


  • @Link150 That's a good thing though

  • Global Moderator

    @RedcommanderV2 No. It is not. You're always all trying to find arguments to keep writing bad code.

    Why can't you just accept advice from scripters who are clearly much more experienced than you are?


  • @Link150 didn't you read what I've said?

    Clearly not, that's not advice, that's clearly forcing people to use the same scripting style as yourself, I actually find doing that very arrogant.

    You have not given me one single good reason that's not opinion based (or based on your logic) why not to use "while wait() do"

    I will not respond to any further message on this because it's you who's stubborn, not us.


  • @Link150

    Does the fact that someone writes code that works mean they're less experienced than you?


  • Okay, let's examine all this more closely.


    You guys say using while wait() do is a "bad practice".

    Now let's take a further look at wait().

    wait() is a YieldFunction. This means this function "doesn't run immediately, and instead takes some time" (Short description).

    Because of this, that while do will need to patiently wait for the wait() to finish running.

    Meanwhile, the code inside that while do won't be running. We accomplished one of the objectives: putting a yield in the while do so it doesn't make the game break.

    Now, what does wait() return? It returns an integer or number, which is the exact (not 100% precise anyway) amount of seconds waited.

    All values except nil and false are considered to be true. With this said, the value returned by wait() will be true-ish. So, we accomplished the second thing: making the condition of our dear while do be met.

    So, using

    while true do
        wait()
    end
    

    is not a bad practice. Neither it is using

    while wait() do
    end
    

    They both work, and none is worse than the other. Because they don't have any negative consequences, both work with no issues, and they don't make the code harder to read either.

    I personally don't use while wait() do, but I don't see anything bad in it.


  • I can't really complain as I used to do this. XP Here's my opinion on it though:

    If the script's kept personal to you, more power to use to do it your way; however, if you plan to release scripts to the public, it might be a good idea to not use wait as the condition for your loops. (Personal irony here, as I used to do have wait as a condition a long time ago.) I'm not saying you have to, but it'd be nice for newbs who're just getting started, and being exposed to scripting for the first time.

    But whatever. Like I said, this's just my opinion.

    TL;DR Do whatever. :P

    Mini Edit

    With all that said and done, I do urge that people not use wait as a condition; you don't have to if you don't want, but I think it'd be nice if people didn't use it as a condition. (At least for the example for newer scripters.)

    Then again, like I stated before, this's just my opinion. I'm not going to tell you whether you should or shouldn't use wait as a condition, as I understand that it's up to personal preference with users.


  • @Aimarekin The problem is, people teach other people this method, and those people teach others, it's an endless cycle. Newbies fall prey to this, unfortunately. Many don't understand why or how it works, they don't know that wait even returns a number. They only use it because they think it's a good idea to blend in with the others. People should either teach good code or not teach at all. One should write code for humans, not for the machines. Programming is not about writing code that just works, it's more about writing self explanatory and good code that works.


  • this is what i don't like about most communities
    all of them try to spread hate even though i just wanted to see what people's opinion were on this

    @Link150, how about you tell us why this and this is bad? Instead of throwing arguments fruitlessly towards another.

    @incapaz i feel like you're just saying this because you have a tough time interpreting "while wait do" as "while the wait duration of _ is completed, do this"

    you still haven't convinced me why, this is just pure opinion more than anything. please don't try to force opinion as fact. you haven't provided any statisical evidence, or any useful refute that isn't just from baseless words.

    people just love to find a way to make things go south.
    :facepalm:


  • @Fifkee What about the newbies? And you're @RedcommanderV2.



  • @incapaz

    How am I RedCommander? Rather, how am I anybody at that? I'm not saying that I encourage using the while wait() loop. You're saying things that are completely opinion, and you still have not given evidence that satiates the question that I asked prior. Why do people say not to use Wait() in a while block? Because some people can't read it? I can read it just fine. If you have as little as an ounce of common sense, you would have the ability to be able to read it as the multiple ways I showed prior to this post. I want an answer that doesn't simply shout "MY CODING ABILITY EXCELS YOURS AND THEREFORE MY SAY GOES." Thats all I want, nothing more. Just because two people surfing the internet doesn't mean that they can instantly say that their say overrides majority. I even agree with most. I have never, EVER seen anyone say that "while wait() do" is bad practice.

    Furthermore, we are not migrating this topic to newbies. We are keeping the topic stagnant.

    How stubborn do you have to be to realize that I do not want opinion, moreso, I want fact?

Log in to reply
 

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