So when I click the next button on it it will play the first time but when I click it again it will not play the next song.
Error:
Players.FuriaI.PlayerGui.mp3player.BackGround.Main:69: bad argument #2 to 'random' (interval is empty)
09:13:23.975 - Stack Begin
09:13:23.976 - Script 'Players.FuriaI.PlayerGui.mp3player.BackGround.Main', Line 69 - global NextSong
09:13:23.977 - Script 'Players.FuriaI.PlayerGui.mp3player.BackGround.Main', Line 81
Script:
01 | local Player = game.Players.LocalPlayer |
02 | repeat wait() until Player.Character |
03 | local Character = Player.Character |
04 | local Bg = script.Parent |
05 | local Bar = Bg.Bar |
06 | local PlayPause = Bg.PlayPause |
07 | local Prev = Bg.Prev |
08 | local Next = Bg.Next |
09 | local SongImg = Bg.ImageLabel |
10 | local Song = Bg.Song |
11 | local Time = Bg.Time |
12 | local SongIsPlaying = false |
13 |
14 | local SoundIds = { |
15 | 897623161 , 844906598 , |
This might be my fault, in part, for giving a lackluster answer yesterday :p
When you pick a song, your ChosenSong
, you remove the song from the array SoundIds. After a couple of removals, there are no ids left in SoundIds, so #SoundIds returns 0. math.Random(1, 0) errors because (1, 0) is an invalid interval. Think about a jukebox; to play a new song, you have to remove the record on the table and choose a new one.
Try adding the song back into SoundIds when you change songs. Something like:
1 | table.insert(SoundIds, ChosenSong [ 1 ] ); |
2 | ChosenSong = { } ; |
3 | -- table.insert(table, value) inserts value at the end of table, so pick an index between the first and second to last values (don't repeat the song) |
4 | local index = math.Random( 1 , #SoundIds - 1 ) |
5 | table.insert(ChosenSong, SoundIds [ index ] ); |
6 | table.remove(SoundIds, index); |