Syncing loops/functions (?)

Example shown below:
https://gyazo.com/93f296b95b42a1970cb6d5d3ec49dd0e
(Game: Splash :star2: Music & Skate)
The GIF shows how each audio asset syncs when enabled at different times.
Basically script waits for a certain beat and then plays the second sound effect to it as well.

Is there a way where I can make a script that waits until a different script has finished it’s loop so a second loop and go along with it?`

Example of my loops:
Motor’s tilting up and down in a 160 BPM (0.375 seconds) waiting time.
https://gyazo.com/ebe2ca2dcae6d1f55eb97993160c02f7
Example 2 of my loops:
Lamp turning on and off in a 160 BPM (0.375 seconds) waiting time.
https://gyazo.com/123fc2ce88c8dd658518f61dcdbf38fe

Combing BOTH effects (end result of this entire post) turns it into this (this is 2 loops enabled at once)
https://gyazo.com/10e00d15f797860ab878c9203cdd1e4c

That was just a showcase of what each loop does and how it looks like when combined.
Back to the question, how can I make a loop wait for the other loop to end before running the second loop along with the first loop? (This question sounds so stupid considering there are 233928 words containing “loop”)

If you don’t know what I mean exactly, I’ll try to explain better.
I know this all may sound confusing as hell.

Demo code would be appreciated.

1 Like

You could create custom events. Essentially just mimick the event system roblox uses or use BindableEvent | Roblox Creator Documentation

Depending on how you have everything setup, you could also just use “Flags”. Where you set a variable to true/false depending on if the loop is finished or started. Something like that.

2 Likes

If I used events, people would be messing with my remotes as far as I know.

I could try with bool attributes to see whether the loop is finished or not.
I’ll let you know if I found a way with it.

2 Likes

Pseudo Example:

local L1Active = false

local loop1 = coroutine.wrap(function()
    L1Active = true
    while(blah)do
        --Some light stuff
    end
    L1Active = false
end)

local loop2 = coroutine.wrap(function()
    while(blah)do
        if(not(L1Active))then continue end
        --This stuff doesn't run unless L1Active = true
    end
end)

loop1()
loop2()
2 Likes

The Bool attribute method (or variable as you called it) helped me.
Thanks :slight_smile:

2 Likes