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

bad argument #2 to 'random' (interval is empty) Why? What?

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

2 comments

0

local pickMusic = math.random(1, playlist)

0

see if that works

1 answer

✓ Accepted answer

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

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