An efficient way to run 2 if statements at the same time

So i been trying to run 2 if statements in a loop but everytime 1 if statement activates the other stops working, how can i fix this?

Use a coroutine: (Documentation - Roblox Creator Hub)

coroutine.resume(coroutine.create(function()
end))

and

spawn(function()
end

are both good examples

ex:

while wait(1) do
   spawn(function()
      if true then
        print("IsTrue")
        wait(2)
      end
    end)
    if true then
      print("IsAlsoTrue")
    end
end

The problem here is that wait(2) does nothing because it is the end of the thread and nothing will resume after the wait ends. Instead there should just be two separate loops in separate coroutines.

The wait(2) represents a yield regardless of what runs and doesn’t run after it, and by executing this code, the script will continue to read out “IsAlsoTrue” while the first one runs. Therefore, this answers the question perfectly without any over technicalities.

To prove this, run it one time the way it is and observe the behavior, then remove the spawn and observe the behavior once again. The one without the spawn coroutine waits 2 seconds before printing “IsAlsoTrue” as its yielded by the wait(2)

The waits in the loop in the OP affect the frequency of the loop, putting the wait in a separate thread from the loop does not. That is the difference I am describing.

1 Like

That’s a reasonable observation. My goal was to explain it from a fundamental approach in order for the OP to come to a valid solution for their exact scenario themselves. Sorry for any confusion :slightly_smiling_face: