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

How can I break a loop from a value change?

Hi there! I am creating a playlist sort of script that allows a player to turn the volume/music on or off. I have a loop created which plays three consecutive songs.

	while true do --For now I just have it in an infinite loop
		song1:Play() --Plays first song
		wait(song1.TimeLength) --Waits until the song is done to go on.
		song1:Stop()-- Just in case :)

		song2:Play() --I think you get the point
		wait(song2.TimeLength)
		song2:Stop()
		
		song3:Play()
		wait(song3.TimeLength)
		song3:Stop() --Loops back
	end	

I'm thinking that I should have a bool value that is changed when a player clicks a button (sound off or on) . I was thinking that I could somehow break the above loop when a player "turns off" the music. Since each song is almost 200 seconds the player shouldn't wait that long before the music turns off, so puttining an if statement in the loop to check the value would be defective.

Value = true --By default, music will be playing

Value.Changed:connect(function()
	if Value == false then--checks if the change turned music off
		break loop --This is where I'm stuck;  how would I break the above loop when the value is changed? 
	end

	if Value == true then --If the music was off then it'll go back true
		loop() --Maybe if the loop was in a funtion? I'm really stumped on the whole task so I dunno XD
	end
end)

Hopefully this is understandable. I hope you guys can give me some suggestions on how I could improve this task!

Thank you!

3 answers

✓ Accepted answer

If you tried to break the loop, you couldn't start it again without adding another loop. Solution, button events. I made your loop a function and added some mouse events based off of GUI buttons to start and stop the music. You could combine them into one "play/stop" or "play/pause" button if you want, but I wont get into that now. The main breakthrough is knowing you can stop songs from anywhere, not just the function or loop where you started them, so that goes into the stop button.

local playingBool = true
local stopButton = path.stopButton
local playButton = path.playButton

local function loopMusic() --this function plays each song sequentially.
	if not playingBool then return end
	song1:Play()
	wait(song1.TimeLength)
	song1:Stop()

	if not playingBool then return end
	song2:Play()
	wait(song1.TimeLength)
	song2:Stop()

	if not playingBool then return end
	song3:Play()
	wait(song1.TimeLength)
	song3:Stop()

	return loopMusic() --start the loop again
end

stopButton.MouseButton1Click:connect(function() --this function stops all songs
	playingBool = false
	song1:Stop()
	song2:Stop()
	song3:Stop()
end)

playButton.MouseButton1Click:connect(function() --this function starts the song loop
	playingBool = true
	loopMusic()
end)

loopMusic() --starts the song loop by default

You could also put your while loop in the function, instead of making it recursive and return the function.

1 comment

0

This works very well! Thank you!

You should be able to use the value to check for when to stop.

playing = true -- changing the name so we don't conflict with Value

function playMusic()
	spawn(function() while playing do
	    song1:Play() --Plays first song
	    wait(song1.TimeLength) --Waits until the song is done to go on.
	    song1:Stop()-- Just in case :)
	
	    song2:Play() --I think you get the point
	    wait(song2.TimeLength)
	    song2:Stop()
	
	    song3:Play()
	    wait(song3.TimeLength)
	    song3:Stop() --Loops back
	end)
end

Value.Changed:connect(function(curValue) -- there is a param you can use here
	playing = curValue
	-- if you want to play the music again just include this
	-- if curValue then playMusic() end
end)

Hope this helps

4 comments

0

I had to make some adjustments, but I got it to work! than you!

0

Its kinda crued but it works, with a little ajustment you should be able to resume playing a song.

0

It actually, keeps playing the next song in the loop even when the value is false ;-;

0

Thats becouseof the way you had the code, the whole code block would need to be completed before it stops

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