Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
10

Why is using wait() as a condition in loops shunned? [closed]

Asked by 5 years ago

I've been gone for a while.

Before I left, everyone openly used wait() as a condition. It was never considered bad and was actually seen as a nice way to omit a single line of code very simply. When I came back, I've been seeing a lot more people contest the idea of using wait() as a condition. Most notably, in while true do loops. Why would that be?

while wait(5) do
    print("I waited ~5 seconds")
end

--Does the same thing ^

while true do
    wait(5)
    print("I waited ~5 seconds")
end

Hack(v.) - to write or refine computer programs or code

Most people argue that doing this is a "hack" with negative connotation. All coding in ROBLOX is considered a hack by definition. Perhaps people are afraid that one day ROBLOX will "patch" this "hack" and all scripts using wait() as a condition will eventually be broken and all games using it will lag!!! But that is simply not the case either. The reason using wait() as a condition works is because things looking for conditions like if then statements and while loops only check if the statement is "truthy" meaning not false or nil. Wait() returns the time it took (in seconds) to finish yielding the rest of the script. You can test that it returns a number:

print(type(wait(3)))

number


So my question is this:

Why do some people refuse to use wait, a number, as a condition? There's a group of people against it, but at the same time, those people would be perfectly fine with a script like this:

local array = {23, nil, 42}

if array[1] then
    print("Exists")
end

but they are against using a number as a condition like so:

while wait(5) do
    print("5 seconds have passed")
end

Is it because wait() yields? Probably not, as most of these people would probably accept this:

script.Parent.Touched:Connect(function(part)
    if part.Parent:WaitForChild("Humanoid") then
        print("Humanoid exists")
    end
end) --WaitForChild was used. It will yield if humanoid does not exist

Fun fact

You can define a variable with Wait() and it will still delay your script. The only difference is your variable is now a number.

while true do
    local a = wait(5)
    print(math.floor(a+20))
end

You can use the fact that the variable is a number in pretty cool ways!

local lighting = game:GetService("Lighting")
local DayStarts = 6*60 --Time of day the game starts at from midnight

while true do
    local OneMinute = wait(0.25) --1 "ROBLOX minute" is how many irl seconds?
    DayStarts = DayStarts+OneMinute/0.25
    lighting:SetMinutesAfterMidnight(DayStarts)
end

Wait() returns 2 variables. I am unsure what the second variable means, that could be a bonus question. My theory is it returns the amount of seconds from 5 pm local time, but it could be wrong.

1
¯\_(?)_/¯ TheeDeathCaster 2368 — 5y
1
The second return value is the delta time. But another argument against while wait() do is for readability. Also some people use it without knowing how or why it works. User#19524 175 — 5y
1
A lot of people use lava brick scripts without knowing how it works. A lot of people use free models even when they don't know how it works. We don't shun new players who use free models or wait as a condition. They're supposed to learn. EzraNehemiah_TF2 3552 — 5y
2
They should learn the right way to do things; not how to abuse hacks or learning old and bad ways. User#19524 175 — 5y
View all comments (13 more)
2
Be a cool kid, and just do `while true do if wait(1) then -- code end end` :> TheeDeathCaster 2368 — 5y
1
bad ^ User#19524 175 — 5y
1
It's the same exact thing as while wait(1) do, just more verbose. EzraNehemiah_TF2 3552 — 5y
1
No. By calling wait in the conditional portion you are using the return value as a condition. Conditions are YES or NO. User#19524 175 — 5y
1
Conditions are not yes or no. They are truthy (not false or nil) or falsey (false or nil). Since "truthy" is so vague, anything can be used as a condition as long as it exists (and by "exists", I don't mean not nil. I mean that if it is a variable, it's a variable that has already been set, or an object in workspace that exists). EzraNehemiah_TF2 3552 — 5y
2
Your conditions *should* be yes or no. How can a number, be interpreted as yes or no (not in Lua)? User#19524 175 — 5y
1
I'm pretty sure wait() would always be counted as true by the script, although the script recognizes that there is also a wait in the line. Forcing the code to pause until the wait finishes. So it likely wasnt a purposely implemented feature, and instead just happens due to wait()'s nature. Another thing to consider, is that many scripters discourage the use of infinetely looping- EtherealTrin 45 — 5y
1
using while loops, as it is usually a good precaution to add a way to break the loop / resume it in case needed for the future. EtherealTrin 45 — 5y
3
I don't think using one is better than using the other. It's only about the preference and maybe some microseconds difference in checking if the return value of wait() is "truthy". It's like arguing if you should use pcall or ypcall Amiaa16 3227 — 5y
1
`wait(5)` a man of culture, i see ;) TheeDeathCaster 2368 — 5y
1
@EtherealTrin, and for this very reason while wait() do works. It abuses the fact that numbers are truthy values and wait, returns a number. Two numbers, to be precise. The dependency is all on the return value of wait. User#19524 175 — 5y
1
@incapaz the return value will never be removed is a point I've made. It never has since the very beginning and I highly doubt it ever will, since that was built in on purpose. EzraNehemiah_TF2 3552 — 5y
0
Main argument against using wait() as your condition is that people tend to forget what the condition is supposed to be used for. Using wait() as a condition and using a conditional statement inside it to break is not a solution to it. As long as you  understand this, the decision is yours to make. zafiruatest 75 — 5y

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
5
Answered by 5 years ago
Edited 5 years ago

In this answer I will attempt to address each of the points you bring up. First of all, I would like you to read this article to see some of the most common reasons it is shunned. Now I will get into my reasoning for why it is and should be (most of the time) shunned. It is really a simple matter, so I will not write too long of an answer. The main reason, in my opinion, for not using wait() as a condition is that it causes people to miss the power of the condition. Like TheeDeathCaster stated in the comments above, you can be the cool kid and do this:

while true do 
    if wait(1) then 
        -- code 
    end 
end

His example is a perfect example of someone missing the point of the condition. You might as well put the wait(1) in the condition of the while loop. The reason people use while wait() do is because it is encouraged by many youtubers and other forums. This encouragement leads new developers who don't know the basics of how a programming language works astray. A perfect example can be found here and you can read my answer to understand my point. I personally do not even encourage infinite loops. You are better off putting a real condition into the loop instead of true or wait(). If you need to have a break in your loop and the condition part of your loop is taken up by wait(), what do you do? You just add an if statement and put a break in there (which is the wrong way to go). In most cases you should be using the condition for something other than wait() or true. Honestly, wait() is just a symbol of an even deeper issue of bad logic and often a lack of proper scripting logic. In my opinion that is why it is shunned and honestly, I think it is a valid point. However, as long as you understand what you are doing, I see no issue with using wait() as a condition. I just ask that you keep it to yourself and try not to confuse the beginning scripter. Also, your note about taking away from the length of your code, that is just a dumb reason. Better code was never written because some guy was like, "Oh, I want shorter code." :

local x=2; local y=2; local function addNumbers(a,b) print(a+b); end; addNumbers(x,y)
-- semicolon just means new line in Lua 

I hope that this expands your understanding of this matter. Have a great day scripting!

1
Note that the logic of loops should be learned before using wait as a condition. After that, it is up to the discretion of the programmer.  User#21908 42 — 5y
2
In fact, the logic of programming should be learned before randomly using conventional methods that other people employ. User#21908 42 — 5y
0
I 100% agree with your comments. EzraNehemiah_TF2 3552 — 5y
Ad