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

How would I get a random id in my table? Please help...!!

Asked by 6 years ago
Edited 6 years ago
Music = {
    ["Bryson Tiller - Been That Way"] = {
        Artist = "Bryson Tiller",
        ItemId = "http://www.roblox.com/asset/?id = 313095574",
        Length = 0 
    },
    ["Drake - God's Plan"] = {
        Artist = "Drake",
        ItemId = "http://www.roblox.com/asset/?id = 1375384985",
        Length = 0  

    },
    ["Bryson Tiller - You Got It"] = {
        Artist = "Brsyon Tiller",
        ItemId = "http://www.roblox.com/asset/?id = 977685900",
        Length = 0
    },

}

So I wanna get a random Item Id in this table. How would I get a random ItemId? Basically, I want it to pull a random Item Id. So then I can still pull the title and the artist of the song. How could this be done? I've been stuck at figuring this out for several minutes.....

0
Use math.random GamingOverlord756 48 — 6y
0
how would I use math.random to get a random value in my table? lol Sharkeedub 179 — 6y
0
It's been awhile since I've done anything to do with lua, but if I'm not mistaken wouldn't it be better to use arrays within an array instead of dictionaries? You'll be able to use the index as an integer rather than a string. You'd then be able to use something like Music[math.random(1, #Music)] AshIReactionZ 15 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

See below for how I would do it.

Music = {
    {
        ['song'] = 'Bryson Tiller - Been That Way',
        ['artist'] = 'Bryson Tille',
        ['itemId'] = 'http://www.roblox.com/asset/?id=313095574',
        ['length'] = 0
    },
    {
        ['song'] = 'Drake - God\'s Plan',
        ['artist'] = 'Drake',
        ['itemId'] = 'http://www.roblox.com/asset/?id=1375384985',
        ['length'] = 0
    },
    {
        ['song'] = 'Bryson Tiller - You Got It',
        ['artist'] = 'Brsyon Tiller',
        ['itemId'] = 'http://www.roblox.com/asset/?id=977685900',
        ['length'] = 0
    }
}

local getRandomSongId = function()
    return  Music[math.random(1, #Music)]['itemId']
end

getRandomSongId()

Personally I would stick to using arrays for the higher levels in the hierarchy and then use a dictionary at the last level.

Hope this helps, if not I'm sure there is away to do it with your current set up.

Ad
Log in to vote
0
Answered by 6 years ago
local pick = Music[math.random(1,#Music)]
0
That won't work because he's using a dictionary. Avigant 2374 — 6y

Answer this question