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!).
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.
I solved it by using the Spawn() function. Thanks to CountOnMeBro for the help!
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)
.