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

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

Asked by 6 years ago

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

0
local pickMusic = math.random(1, playlist) hellmatic 1523 — 6y
0
see if that works hellmatic 1523 — 6y

1 answer

Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

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
Ad

Answer this question