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

Hello! I was working on a randomizer and well this error came up even though there was data put into the table… Can you guys take a look at it and see what I did wrong? :slight_smile:

 -- << ITEMS >>
contents.Items = {
	["crate1"] = {
		["Common"] = {
			["Gym Ghost"] = {Item = GhostBoxOne.GymGhost; Color = Common; Name = "Gym Ghost"};
			["Life Guard"] = {Item = GhostBoxOne.LifeGuardGhost; Color = Common; Name = "Life Guard"};
			["Halo"] = {Item = GhostBoxOne.AngelGhost; Color = Common; Name = "Halo"};
			["King Ghost"] = {Item = GhostBoxOne.KingGhost; Color = Common; Name = "King Ghost"};
		};
		
		["Uncommon"] = {
			["Blue Beanie"] = {Item = GhostBoxOne.BlueBeanieGhost; Color = Uncommon; Name = "Blue Beanie"};
			["Top Hat"] = {Item = GhostBoxOne.TopHatGhost; Color = Uncommon; Name = "Top Hat"};
			["Bear"] = {Item = GhostBoxOne.BearGhost; Color = Uncommon; Name = "Bear"};
		};
		
		["Rare"] = {
			["Army"] = {Item = GhostBoxOne.ArmyGhost; Color = Rare; Name = "Army"};
			["Penguin"] = {Item = GhostBoxOne.PenguinGhost; Color = Rare; Name = "Penguin"};
		};
		
		["Legendary"] = {
			["Shadows"] = {Item = GhostBoxOne.DarkRavenGhost; Color = Legendary; Name = "Shadows"};
		};
	}
	
}




contents.Pick = function(box)
	if box == "crate1" then
		local randomNumber = math.random(1,100)
		
		local counter = 0
		
		for rarity, weight in pairs(contents.rarities) do
			counter = counter + weight 
			
			if randomNumber <= counter then
				local rarityTable = contents.Items.crate1[rarity]
                                --Error is here
				local chosenItem = rarityTable.Name[math.random(1,#rarityTable)]
                               --Error is here

				return chosenItem
				
				
			end
		end
	end--End of box1 function
	
	
	
	
	
end--End of first function
	

Thank you! :smiley:

2 Likes

I think it may be because you are trying to get the amount of items in a dictionary which uses a string index as opposed to a table which uses a number index.

For example, if you do this, it will print 0 even though there are two entries.

print(#{['a'] = 'b'; ['b'] = c})
1 Like