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
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
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
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.
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!
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?