Looping two things at one time

I’m trying to make a phone decoration that plays a ringing sound on loop, and also blinks a red light during the period that the sound is playing. This is what I have at the moment,

local Sound = script.Parent
local LightPart = Sound.Parent.Parent.LightPart

local function Blink()
	repeat
		LightPart.Material = Enum.Material.Neon
		wait(1)
		LightPart.Material = Enum.Material.SmoothPlastic
		wait(1)
	until not Sound.IsPlaying
end

while true do
	script.Parent:Play()
	Blink()
	wait(10)
	script.Parent:Stop()
	wait(10)
end

but the sound doesn’t stop playing after 10 seconds, and the light continues to blink, presumably because the script is stuck in the repeat loop, how do I fix this?

I’d suggest trying to replace Sound.IsPlaying with Sound.IsPaused because it also works when you call the function Sound:Stop()

Does IsPaused() return when I call Stop()? I thought it only worked for Sound.Pause(). But I suppose, the documentation states that it returns when a sound “isn’t playing”.

Hello, you can solve this by starting coroutine, which is executed in different thread.
Best solution for this would be making two coroutines so the main thread has time to do different things.

local soundCoroutine = coroutine.create(function()
    script.Parent:Play()
    wait(10)
    script.Parent:Stop()
    wait(10)
end)

local blinkCoroutine = coroutine.create(function()
    repeat
        LightPart.Material = Enum.Material.Neon
        wait(1)
        LightPart.Material = Enum.Material.SmoothPlastic
        wait(1)
    until not Sound.IsPlaying
end)

coroutine.resume(soundCorotiine)
coroutine.resume(blinkCoroutine)

This is untested code, tweak it and it should work properly.

1 Like