Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 7 years ago

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?

0
Yes. It basically loops until true equals false, which obviously will never happen. Perci1 4988 — 7y
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! saenae 318 — 7y

1 answer

Log in to vote
3
Answered by 7 years ago

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.

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 saenae 318 — 7y
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. saenae 318 — 7y
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 ;) RockerCaleb1234 282 — 7y
0
Ah ok, I misread as a simple "while loop", rather than specifically "while true do". Sorry about that! xD saenae 318 — 7y
Ad

Answer this question