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

How To Fix Error, "bad argument #2 to 'random' (interval is empty)?"

I am trying to make a crate system. I have Commons, Rares, Legends, and Godly's. Everything unboxes just fine but when I try to unbox the Godly Crate This Error Shows, " . Allbad argument #2 to 'random' (interval is empty)" Crates run through the same script, so I don't get why it does this. Help if you can! Here is the part of the codes that shows the error,

local Choose = math.random(1,100);
				
				
				local ChanceTable = {};
				for Rarity,Chance in pairs(Chances) do
					for i = 1,Chance do 
						table.insert(ChanceTable,Rarity);
					end
				end;
				
				SelectedRarity = ChanceTable[math.random(1,#ChanceTable)];
				SelectedKnife = ItemTable[SelectedRarity][math.random(1,#ItemTable[SelectedRarity])];  -- Shows the error on this line	

				ChanceTable = {};

2 comments

0

That doesn't really help, all crates open but one specific one. And they are all ran through one script.

1 answer

there are two ways math.random #2 will be an error

  1. the 2nd argument is nil, that is obvious
  2. some how, you did not forsee, but when the game starts to run, the 2nd argument is less than the first argument. from your error, I can see that your table was empty {}, so the #table is 0. because 1 > 0, random failed.

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