Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can sombody help me with my sound problem?

I'm not much of a scripter but i did learned how to make a music button for my background music off the wiki

local button = script.Parent
local sound = button:WaitForChild('Sound') 

sound:Play()



local function onButtonClick()
	if sound.IsPlaying then
		button.Image = 'http://www.roblox.com/asset/?id=166376559'
		sound:Stop()
	else
		button.Image = 'http://www.roblox.com/asset/?id=166376578'
		sound:Play()
	end
end

button.MouseButton1Click:connect(onButtonClick) 

right now only the first 120 second sound plays but I have 2 sounds that go together but are split up into 2 parts because 120 second limit. I put 2 sounds in ImageButton called sound and sound2 now i need sound 2 to play right after sound 1 and then after sound 2 is done sound 1 plays again Would this work adding a loop:

local button = script.Parent
local sound = button:WaitForChild('Sound') 

while true do
    sound:Play()
    wait(120)
    sound2:Play()
    wait(50)
end

local function onButtonClick()
	if sound.IsPlaying then
		button.Image = 'http://www.roblox.com/asset/?id=166376559'
		sound:Stop()
	else
		button.Image = 'http://www.roblox.com/asset/?id=166376578'
		sound:Play()
	end
end
 
button.MouseButton1Click:connect(onButtonClick) 

I don't think that's right at all because i don't know what i'm doing but i would appreciate it if someone could help me out.?

1 answer

You are on the right track, but making multiple sounds complicates this problem a bit.

The best way to do this in my opinion is to put the sounds you want to loop through in a table. This way we can loop through each of the sounds and play them in order without needing to explicitly declare sound1, sound2, etc.

We will need to write a function that will loop through the sounds over and over until the button is clicked. To do this, we will start by declaring a variable shouldPlay that we can set to false to break the loop.

In this function we will need a while loop, written as while shouldPlay do. Inside of this loop we will need to go through every sound and play them, which we can do with a generic for loop.

For each sound we call Play on, we will need to wait until it is done playing OR when it should no longer play (shouldPlay == false). We can check for this using a repeat loop that waits a very small amount of time between each check.

Now if shouldPlay does get set false, we need to break out of the for loop. Once this happens the while loop will check its condition and also end, breaking out of the entire looping function.

local button = script.Parent
local sounds = {  -- table of sounds
	button:WaitForChild("Sound") ,
	button:WaitForChild("Sound2")
}
local shouldPlay = true

local function loopSounds()
	while shouldPlay do
		for _, sound in ipairs(sounds) do -- For every sound in the table
			sound:Play()

		-- wait until the sound is done playing or shouldPlay is set to false
			repeat
				wait()
			until not sound.IsPlaying() or not shouldPlay

			-- if shouldPlay is false break out of the for loop
			if not shouldPlay then
				sound:Stop()
				break
			end
		end
	end
end

button.MouseButton1Click:connect(function() -- anonymous function
	if not shouldPlay then -- 'Turn on' loop
		button.Image = 'http://www.roblox.com/asset/?id=166376559'
		shouldPlay = true
		loopSounds() -- Loop through sounds instead of playing one
	else -- 'Turn off' loop
		button.Image = 'http://www.roblox.com/asset/?id=166376578'
		shouldPlay = false -- Break the loop
	end
end)

loopSounds() -- Make it so it starts off playing

3 comments

0

Is there a reason you can't use one of the Sound events (e.g. Ended) that makes you write a repeat loop yourself?

0

pairs/next is unordered. Since the sounds is a list and the elements have an order, you want to use ipairs.

0

Ah good point. And I would use Ended, but I also wanted it to stop playing once the varaible shouldPlay was set to false (didn't want to "complicate" it with a BindableEvent).

This discussion is preserved as it was. New answers, comments, and votes are closed.