How to run 2 different infinite loops at the same time

As the title says I want to run two different while true do loops at the same time. I think this can be made using coroutines but I really dont understand them I just need a simple example of how to run 2 different infinite loops at the same time.

5 Likes

Coroutines should work, but spawn() is a more simple form yet worse performing than coroutines.

coroutine.wrap(function()
     -- insert code here
end)()

This is the simple structure that I usually use. Then after you set the coroutine, add the other loop. I’m puzzled about why you’re splitting up two loops. Care to elaborate?

12 Likes

Thanks for helping and the reason I have 2 loops is that one is to constantly update the guns and viewmodels Cframe and the second is for shooting and since there will be a fire rate for the gun then if I put them all in one loop update will happen with every time you shoot and It will look like the game is freezing

I think the alternate solution is to move the entire viewmodel updates as low down in the script as possible and let the firing be in a function, where the player holds down the button and a new signal is sent to the function through connection, until the button is released.

yeah that can work too and I just realized I can make the weapons shooting from a different script in each model since firing is controlled by a module. So i will make each weapons firing sytem from a script in them. This will also allow me to make every gun custom stats easier.

Thanks for the help tho.

1 Like

This seems like an anti-pattern to me.

1 Like

Thanks but I already used a function so that wait happens outside the loop.

1 Like

tysm, learned it like 6 months ago but now I bethink it (I needed to make 4 different loops to update player’s currencies and only one worked and that was bad)

Thank you for helping me with my issue, I really need it!!