I have a sound in the Workspace with a script in. I have a Table / Arrary inside the script containing 3 music IDs. How do I make sure the same song doesn't play again after it playing? Here :
1 | Music = { 177226555 , 156637449 , 186747495 } |
2 |
3 | while true do |
4 | script.Parent:Stop() |
5 | script.Parent.SoundId = "" |
6 | script.Parent.SoundId = "http://www.roblox.com/Asset/?id=" ..Music [ math.random( 1 ,#Music) ] |
7 | script.Parent:Play() |
8 | wait(script.Parent.TimeLength) |
9 | end |
To stop that, just store the last played ID in a variable, and check if the random ID is the same as the one in the variable. This should work:
01 | Music = { 177226555 , 156637449 , 186747495 } |
02 |
03 | local Last -- Declare Last |
04 |
05 | while true do |
06 | script.Parent:Stop() |
07 | script.Parent.SoundId = "" |
08 | local RandomId -- Declare RandomId |
09 | repeat RandomId = Music [ math.random( 1 ,#Music) ] until Last~ = RandomId |
10 | Last = RandomId |
11 | script.Parent.SoundId = "http://www.roblox.com/Asset/?id=" ..RandomId |
12 | script.Parent:Play() |
13 | wait(script.Parent.TimeLength) |
14 | end |
Hope it helps! Comment if you don't understand.
A good thing to do would be to store the currently playing song in a variable and remove it from the table, then put it back after choosing the next one.
01 | local Music,song = { 177226555 , 156637449 , 186747495 } |
02 | while true do |
03 | local i = table.remove(Music,math.random(#Music)) |
04 | Music [ #Music+ 1 ] = song |
05 | song = i |
06 | script.Parent:Stop() |
07 | script.Parent.SoundId = "rbxassetid://" ..song |
08 | script.Parent:Play() |
09 | wait(script.Parent.TimeLength) |
10 | end |