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

Does While true do work as a loop? Im new to Scripting!

Can anyone please help me with my Loop Script? I need help with a music playist on my game!

while true do
	
	game.Workspace.MusicOne:Play() --this will play 'MusicOne
	wait(119) --the time inbetween each song
	game.Workspace.MusicTwo:Play()
	wait(119)
	game.Workspace.MusicThree:Play() --this will play 'MusicOne
	wait(119)
	game.Workspace.MusicFour:Play() --this will play 'MusicOne
	wait(119)
	game.Workspace.MusicFive:Play() --this will play 'MusicOne
	wait(119)
	game.Workspace.MusicSix:Play() --this will play 'MusicOne
	wait(119)
end

This is what i think is a loop script will this loop it?

2 comments

0

Yes. It basically loops until true equals false, which obviously will never happen.

0

As a just-in-case your songs aren't necessarily 119 seconds long, I would recommend using the Sound Instance's TimeLength property in your waits. In example, after playing your first song, you could use: wait(game.Workspace.MusicOne.TimeLength); in place of wait(119). Hope that helps somewhat!

1 answer

✓ Accepted answer

If you are looking for something to loop and never end while the script is enabled then you will probably want to use the while true do loop like you did in that example. If you are looking for something that will keep looping, but will end if something certain happens then you would want a repeat loop like this:

repeat
	workspace.MusicOne:Play()
	wait(119)
	workspace.MusicTwo:Play()
	wait(119)
	workspace.MusicThree:Play()
	wait(119)
	workspace.MusicFour:Play()
	wait(119)
	workspace.MusicFive:Play()
	wait(119)
	workspace.MusicSix:Play()
	wait(119)
until game.Mute.Value == false -- some random example :l 

You can also mess around with if statements and the Break command to stop a loop if needed.

Please remember to accept my answer if you feel it helped answer your question. If not then please comment on how I could have done better.

4 comments

0

Actually, the while loop doesn't necessarily go on forever, it's still conditional. In example, you could've said "while game.Mute.Value do", and that would've had the same effect as your repeat until. However, the real difference between a repeat loop and a while is that the while loop will check the condition first thing, and then break if that condition is false. The repeat loop, on the other h

0

Sorry, post was too long. xD Anyway, yeah, the repeat loop will run at least once before breaking, since it doesn't check the condition until after running.

0

I put in italics, "while true do" referring to if you set up the while loop to perform whatever while true == true. If I am misunderstanding then let me know ;)

0

Ah ok, I misread as a simple "while loop", rather than specifically "while true do". Sorry about that! xD

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