So here is my code:
local playlist = { stargazing = { ["Name"] = "Kygo - Stargazing ft. Justin Jesso", ["ID"] = "rbxassetid://1067101081", ["Info"] = "This music can't be used on YouTube." }, skyhigh = { ["Name"] = "JJD - Skyhigh", ["ID"] = "rbxassetid://271028838", ["Info"] = "This music can be used on YouTube." }, allfallsdown = { ["Name"] = "Alan Walker - All Falls Down", ["ID"] = "rbxassetid://1163496902", ["Info"] = "This music can't be used on YouTube." } } local music = game.Workspace:WaitForChild("Music") local playing = {} while wait(5) do print(#playlist) if music.IsPlaying == false then local pickMusic = math.random(1, #playlist) playing = playlist[pickMusic] local notif = Instance.new("Hint", game.Workspace) notif.Name = "MusicNotif" notif.Text = "Now playing: "..playing.Name.." - "..playing.Info music.SoundId = playing.ID music:Play() end end
Here is the output:
0 15:12:44.287 - ServerScriptService.Scripts.Playlist:28: bad argument #2 to 'random' (interval > is empty) 15:12:44.287 - Stack Begin 15:12:44.288 - Script 'ServerScriptService.Scripts.Playlist', Line 28 15:12:44.288 - Stack End
In your code, the playlist
table is a dictionairy. Using the #
operator on a dictionairy table will always return 0 unless it has some number keys.
Make it a normal table instead:
local playlist = { { ["Name"] = "Kygo - Stargazing ft. Justin Jesso", ["ID"] = "rbxassetid://1067101081", ["Info"] = "This music can't be used on YouTube." }, { ["Name"] = "JJD - Skyhigh", ["ID"] = "rbxassetid://271028838", ["Info"] = "This music can be used on YouTube." }, { ["Name"] = "Alan Walker - All Falls Down", ["ID"] = "rbxassetid://1163496902", ["Info"] = "This music can't be used on YouTube." } } local music = game.Workspace:WaitForChild("Music") local playing = {} while wait(5) do if music.IsPlaying == false then local pickMusic = math.random(1, #playlist) local notif = Instance.new("Hint", game.Workspace) notif.Name = "MusicNotif" notif.Text = "Now playing: "..pickMusic.Name.." - "..pickMusic.Info music.SoundId = pickMusic.ID music:Play() end end