Invalid argument #2 to 'random' (interval is empty)

The issue is on line local ChosenColor (but the higher one)


I believe that error means the upper limit is below the lower limit.

1 Like

Yeah and to expand on that, you (OP @Philipceo90) have keys which are assigned to values in your table which means using the length operator (#) wouldn’t work.

This means you’d manually have to go thru the table and count how many keys there are in order to get the length.

I forgot tables with keys were called dictionaries. Thanks to @paetemc2 for pointing that out.

1 Like

I believe this is because, #AvalibleColors is returning 0, because that syntax doesn’t work the way you think it does on dictionaries, only on tables.



local function dictionaryLength(tbl)
	local count = 0
	for _, value in pairs(tbl) do
		count += 1
	return count

local ChosenColor = AvailableColors[math.random(dictionaryLength(AvailableColors))]
1 Like

so how would i go on fixing this?

The reason this is happening, as others have mentioned, is because the size of a dictionary is undefined.

I see many people recommending a “getTableSize” method, stop it, thats inefficient as hell, considering I assume the table is a fixed definition, in which case you should be able to just define a “length” variable next to it

local AvailableColorsSize = 5

Although from what you’re trying to do, you’d be better off scraping the dictionary entirely and just use a list of structs

local AvailableColors = {
  {Color = "Red", InUse = false}