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

How can I run a loop while another loop is already running at the same time?

Asked by 2 years ago
Edited 2 years ago

Hey there. How can I run a loop (like > for) while another loop in the same script running at the same time? Like, a part will move in X direction and the same part will also rotate 90 degrees in Z direction at the same time. The problem is, I don't wanna run them in the same loop because I wanna repeat the move command 20 times and rotate thing only 6 times. I'm kinda new to scripting, though. Let me explain, here's the moving script:

for i = 1,20 do
      script.Parent.Position = script.Parent.Position + Vector3.new(1,0,0)
      wait(0.2)
end

and here's the rotation (Orientation) one:

for i = 1,6 do
    script.Parent.Orientation = script.Parent.Orientation + Vector3.new(0,0,15)
    wait(0.2)
end

as I said, I want them to repeat different times(move loop 20 times and rotation one 6 times) so I can't put them in the same loop. Putting them in an order will cause the script do it one by one; it'll move first, and when it's complete it'll start to rotate but I don't want that. I want the script to do those things at the same time. I hope you understand! (idk if it's possible in Lua, but I would be very happy if you help me!).

3 answers

Log in to vote
0
Answered by 2 years ago

Definitely not the most organized answer but I would just imbed an if statement to see if i <= 6 and rotate the other part if it is. That would look like this:

for i = 1,20 do
      script.Parent.Position = script.Parent.Position + Vector3.new(1,0,0)

      if i <= 6 then
            script.Parent.Orientation = script.Parent.Orientation + Vector3.new(0,0,15)
      end
      wait(0.2)
end

again, this is just how I would do it without learning any new material. Hope I helped.

Please let me know if you're reading this and you have a method that is intended to be used in situations such as these.

0
That's also a great way to do it, thank you so much for the help! But CountOnMeBro (thanks to him btw) told me to use Spawn() function instead. It's a better way to do the same thing, but thank you so much for the help. But as a sollution I'm gonna accept your answer, because it also works too. Thanks again! Va1t_Dev 86 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

I solved it by using the Spawn() function. Thanks to CountOnMeBro for the help!

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
2 years ago

I know you have found your answer but I also want to make you aware of coroutine. it’s basically spawn but more reliable and has more features. contrary to popular belief, you can still get traceback using debug.traceback(thread).

0
Thank you for informing me about that function. I'll learn about it! Va1t_Dev 86 — 2y

Answer this question