So I have a table and I want a random thing to be chosen from the table but every once in a while it chooses nil instead of a number to look for in the table.

chance = {1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,4}
clen = 0
for k, v in pairs(chance) do
    clen = clen + v
end

while wait(30) do
	a = chance[math.random(1,clen-1)]
	warn(a) -- This sometimes returns nil instead of a number
	if a == 1 then
		newNormalBox(math.random(50,500))
	else if a == 2 then
		newGemBox(math.random(1,3))
	else if a == 3 then
		newGoldBox(math.random(2000,4000),math.random(1,10))
	else if a == 4 then
		newDiamondBox(math.random(20000,50000),math.random(5,40))
	end
	end
	end
	game.StarterGui:SetCore("SendNotification",{
		Title = "NOTICE"; 
		Text = "A box has spawned somewhere in the map. You should go see what's inside!";
		Icon = "";
		Duration =  5;
	})
	end
end

3 comments

1

Try: "clen = #chance" I have no idea what you're trying to do with that summation loop o'er there

0

I am getting the length of chance

0

oh I see