What is the difference between these two, which one is more optimized?

while x do
repeat wait() until x == false

If there is something I can use instead of these two, write it down please.


They dont have any difference expect that repeat until will atleast run once. e.g

local x = true
repeat print("Hi"); task.wait() until x --print "Hi" once only

whereas while loop can be customizable for condtitions

local x = false
while x do 
  print("Hi") --Hi will never be printed


The difference is that with while, the condition is checked before execution while with repeat until the condition is checked after if I remember correctly.

What zayer and david said is correct, although fyi if you can, instead of waiting until something turns true, you should try running your code in the part where it would turn it true, or make it event based.

(Meant to reply to the post oops)

1 Like

my goal is there are 2 different scripts 1. after the start, the second one will run

You could try using bindable events, which the first script fires and the second script connects to

Try taskwait()
it makes it so that it won’t be exactly the amount as specified (it might be off by like 0.001 second) but by doing just wait() will force the program to stop waiting EXACTLY the moment x == false pressuring the system. Doing taskwait() basically makes your game lag less! (ik someone already said this I am just explaining it!)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.