How to make math.random in table

So I’m trying to make the table pick a random item from the talbe, but it doesn’t work because I have 2 values and it should only pick the name of the color.

rarityTable[math.random(1,#rarityTable)]
["Common"] = {
		["White"] = tostring(Color3.fromRGB(255,255,255)),

or it could look like this

["Common"] = {
		White = Color3.fromRGB(255,255,255),
local colors = {}
for _, rarity in pairs(rarityTable) do
	for name, _ in pairs(rarity) do table.insert(colors, name) end
end

local color = colors[math.random(#colors)]
print(color)
1 Like

I guess you’re trying to get a random element in the rarityTable?

Since the ‘table’ is a table containing string indexes, it is essentially a dictionary.

So, in other words, you’re trying to get a random element in a dictionary.

You could have another table which stores the indexes.

local rarityIndexes = {'Common'}
local rarityColorIndexes = {
     {'White'},
}

local randomRarity = rarityIndexes[math.random(1, #rarityIndexes)]
local randomColorFromRarity = rarityColorIndexes[table.find(rarityIndexes, randomRarity)]
1 Like

try this instead:

rarityTable[math.random(1, #rarityTable[1])]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.