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

bad argument #2 to 'random' (interval is empty) how do I fix this? and what is an interval?

So I have a table here

local TABLE = {
    One = 1;
    Two = 2;
    Three = 3;
}

local RANDOM_ELEM = math.random(1, #TABLE)
print(RANDOM_ELEM)

Line 7 gives me the error.

2 answers

✓ Accepted answer
local TABLE = {1, 2, 3}

local RANDOM_ELEM = math.random(1, #TABLE)
print(RANDOM_ELEM)

Removing the Keys for each of them should fix the problem

(normally index values would look like ["One"], although testing with that didn't seem to work)

4 comments

0

Thx! Also how many more questions do I ask? To get you to 2000?

0

Well if you calculated it would be (2000 - 1172) / 15 is approx. 56. Of course, I'll answer any question if I'm able to that hasn't already been answered, though its not guaranteed.

0

Huh, I saw you at 800 two days ago, I am falling behind:)

0

Oh it worked the button showed up. Enjoy some reputation!

That is a dictionary, not a table. You will need to write your own custom function to measure the length of a dictionary. I just made a simple one for you.

Code:

local TABLE = {
    One = 1;
    Two = 2;
    Three = 3;
}


function MeasureLength(x)
	local l = 0
	for _,t in pairs(x) do
		l = l+1
	end
	return l
end

local length = MeasureLength(TABLE)
local RANDOM_ELEM = math.random(1, length)
print(RANDOM_ELEM)

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