Jump to content

Bad argument #2 on 'random' (interval is empty)


drk

Recommended Posts

Line 107 ( the line of the error ):

      local random = math.random(1, #musics) 

#musics is:

     local musics = { 
        [ 'David Guetta - Where Dem Girls At' ] = 'http://199.167.195.194/wdga.mp3', 
        [ 'Coldplay - Paradise' ] = 'http://199.167.195.194/pd.mp3', 
        [ 'Coldplay - Viva la Vida' ] = 'http://199.167.195.194/vlv.mp3', 
        [ 'Ana Malhoa - Danza Kuduro' ] = 'http://199.167.195.194/dk.mp3', 
        [ 'Klaas - The Way' ] = 'http://199.167.195.194/tw.mp3', 
        [ 'Basshunter - All I ever wanted' ] = 'gfx/music/bh-aiew.mp3', 
        [ 'Whiz Khalifa - Black and Yellow' ] = 'gfx/music/wk-bay.mp3', 
        [ 'Skrillex - My name is Skrillex ( Remix )' ] = 'gfx/music/sx-mnis.mp3', 
        [ 'Italobrothers - Stamp on the Ground' ] = 'gfx/music/ib-sotg.mp3', 
        [ 'Madcon - Freaky like Me' ] = 'gfx/music/mc-flm.mp3', 
        [ 'Cut the Music - Greatest Deejay' ] = 'gfx/music/ctm-gd.mp3', 
        [ 'No Name - Time`s Vortex' ] = 'gfx/music/tv.mp3', 
        [ 'Manian - Welcome to the Club' ] = 'gfx/music/wttc.mp3', 
        [ 'Bon Jovi - It`s my Life' ] = 'gfx/music/iml.mp3', 
        [ 'Example - Changed the Way you Kissed Me' ] = 'gfx/music/ctwykm.mp3', 
        [ 'Example - Kickstart ( Bar 9 Remix )' ] = 'gfx/music/e-ksb9rx.mp3', 
        [ 'Ellie Goulding - Lights ( Remix )' ] = 'http://199.167.195.194/Lights.mp3', 
        [ 'Arkasia - New World Disorder' ] = '', 
        [ 'ATB - You are not Alone ( Remix )' ] = '' 
    } 

Why I get this error?

Link to comment
  
local musics = { 
        [ 'David Guetta - Where Dem Girls At' ] = 'http://199.167.195.194/wdga.mp3', 
        [ 'Coldplay - Paradise' ] = 'http://199.167.195.194/pd.mp3', 
        [ 'Coldplay - Viva la Vida' ] = 'http://199.167.195.194/vlv.mp3', 
        [ 'Ana Malhoa - Danza Kuduro' ] = 'http://199.167.195.194/dk.mp3', 
        [ 'Klaas - The Way' ] = 'http://199.167.195.194/tw.mp3', 
        [ 'Basshunter - All I ever wanted' ] = 'gfx/music/bh-aiew.mp3', 
        [ 'Whiz Khalifa - Black and Yellow' ] = 'gfx/music/wk-bay.mp3', 
        [ 'Skrillex - My name is Skrillex ( Remix )' ] = 'gfx/music/sx-mnis.mp3', 
        [ 'Italobrothers - Stamp on the Ground' ] = 'gfx/music/ib-sotg.mp3', 
        [ 'Madcon - Freaky like Me' ] = 'gfx/music/mc-flm.mp3', 
        [ 'Cut the Music - Greatest Deejay' ] = 'gfx/music/ctm-gd.mp3', 
        [ 'No Name - Time`s Vortex' ] = 'gfx/music/tv.mp3', 
        [ 'Manian - Welcome to the Club' ] = 'gfx/music/wttc.mp3', 
        [ 'Bon Jovi - It`s my Life' ] = 'gfx/music/iml.mp3', 
        [ 'Example - Changed the Way you Kissed Me' ] = 'gfx/music/ctwykm.mp3', 
        [ 'Example - Kickstart ( Bar 9 Remix )' ] = 'gfx/music/e-ksb9rx.mp3', 
        [ 'Ellie Goulding - Lights ( Remix )' ] = 'http://199.167.195.194/Lights.mp3', 
        [ 'Arkasia - New World Disorder' ] = '', 
        [ 'ATB - You are not Alone ( Remix )' ] = '' 
    } 
  
local themusic = musics[math.random(0, #musics)] 
  

Link to comment

Because you changed the indexes of the table, the table should be formatted this way

local musics = { 
        [1] = { 'David Guetta - Where Dem Girls At', 'http://199.167.195.194/wdga.mp3'}, 
        [2] = { 'Coldplay - Paradise', 'http://199.167.195.194/pd.mp3'}, 
        [3] =  
... 
} 
  
local themusic = musics[math.random(#musics)] 
local file = themusic[2] 
local title = themusic[1]  

Link to comment

Hey, now I get this error on line 103: attemp to index field '?'(a nil value)

line 113:

        currentSound = playSound(theSound, true) 

theSound variable is

        local theSound = sounds[math.random(2, #randoms)] 

When the music will change I get this error and the music started plays again =s

Edit: Oh, and the #randoms table is

    local randoms = { 
        'http://199.167.195.194/wdga.mp3', 
        'http//199.167.195.194/pd.mp3' 
    } 

Edited by Guest
Link to comment

Double click play sound:

        addEventHandler('onClientGUIDoubleClick', root, 
      function() 
          if source == music_grid then 
            local row, col = guiGridListGetSelectedItem(source) 
            if row and col and row ~= -1 and col ~= -1 then 
              local name = guiGridListGetItemText(source, row, music_grid_column) 
              local song = guiGridListGetItemData(source, row, music_grid_column) 
              if isElement(currentSound) then 
                destroyElement(currentSound) 
              end 
              currentSound = playSound(song, true) 
              guiSetText(music_name, tostring(name)) 
            else 
              if not source then 
                setRandomMusic() 
              end 
            end 
        end 
    end 
    ) 

Tables:

    local musics = { 
        [ 'David Guetta - Where Dem Girls At' ] = 'http://199.167.195.194/wdga.mp3', 
        [ 'Coldplay - Paradise' ] = 'http://199.167.195.194/pd.mp3', 
        [ 'Coldplay - Viva la Vida' ] = 'http://199.167.195.194/vlv.mp3', 
        [ 'Ana Malhoa - Danza Kuduro' ] = 'http://199.167.195.194/dk.mp3', 
        [ 'Klaas - The Way' ] = 'http://199.167.195.194/tw.mp3', 
        [ 'Basshunter - All I ever wanted' ] = 'gfx/music/bh-aiew.mp3', 
        [ 'Whiz Khalifa - Black and Yellow' ] = 'gfx/music/wk-bay.mp3', 
        [ 'Skrillex - My name is Skrillex ( Remix )' ] = 'gfx/music/sx-mnis.mp3', 
        [ 'Italobrothers - Stamp on the Ground' ] = 'gfx/music/ib-sotg.mp3', 
        [ 'Madcon - Freaky like Me' ] = 'gfx/music/mc-flm.mp3', 
        [ 'Cut the Music - Greatest Deejay' ] = 'gfx/music/ctm-gd.mp3', 
        [ 'No Name - Time`s Vortex' ] = 'gfx/music/tv.mp3', 
        [ 'Manian - Welcome to the Club' ] = 'gfx/music/wttc.mp3', 
        [ 'Bon Jovi - It`s my Life' ] = 'gfx/music/iml.mp3', 
        [ 'Example - Changed the Way you Kissed Me' ] = 'gfx/music/ctwykm.mp3', 
        [ 'Example - Kickstart ( Bar 9 Remix )' ] = 'gfx/music/e-ksb9rx.mp3', 
        [ 'Ellie Goulding - Lights ( Remix )' ] = 'http://199.167.195.194/Lights.mp3', 
        [ 'Arkasia - New World Disorder' ] = '', 
        [ 'ATB - You are not Alone ( Remix )' ] = '' 
    } 
  
    local randoms = { 
        'http://199.167.195.194/wdga.mp3', 
        'http//199.167.195.194/pd.mp3' 
    } 

setRandomMusic function:

    function setRandomMusic () 
        local theSound = randoms[math.random(#randoms)] 
        currentSound = playSound(theSound, true) 
        setTimer(currentSound, 1000, 1) 
        guiSetText(music_name, tostring(name)) 
    end 

It's not the full code, only a part.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...