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

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

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.....

3 comments

0

Use math.random

0

how would I use math.random to get a random value in my table? lol

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)]

2 answers

✓ Accepted answer

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.

local pick = Music[math.random(1,#Music)]

1 comment

0

That won't work because he's using a dictionary.

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