Looking for a method for math.random() to select a number from a range, even if the range's maximum is equivalent to its minimum

Good day! I’m looking for any method to mitigate the invalid argument #2 to 'random' (interval is empty) in a math.random() function. What I’m trying to go for here is an item ‘deletion’ from its shelf. However, when the number of objects in the table reaches 1, which is the same as the minimum value of the range in math.random(), it returns the aforementioned error. What I want to happen is that, regardless of range, it will choose whatever is available from the range. Many thanks to anyone who can articulate and address my concern!

Code:

	local clickParts = shelf.Click:GetDescendants() -- Shelf deletion by player

	for i,v in ipairs(clickParts) do
		if v:IsA("ClickDetector") then
			v.MouseClick:connect(function()
				local itemsTable = v.Parent.Parent.Parent:FindFirstChild("Items"):GetChildren()
				local itemToBeDestroyed = itemsTable[math.random(1, #itemsTable)] -- ISSUE: Upper limit == Lower limit when 1 instance remains under itemsTable
				itemToBeDestroyed:Destroy()
				itemsLeft.Value -=1  
				print(#itemsTable)
			end)
		end
	end```

Try this: itemsTable[math.random(#itemsTable)] instead of using 1 just use the length of table. It should work.

1 Like

The issue still persists, though thank you for conveying your assistance!

I think you should just empty the table unless you need it to manually loop through. Or just use :ClearAllChildren()

1 Like

Why not test if its 1 like:

				local itemToBeDestroyed = #itemsTable == 1 and  itemsTable[1] or itemsTable[math.random(1, #itemsTable)]
1 Like

Try this (excuse the formatting, on mobile)


local itemToBeDestroyed = itemsTable[#itemsTable == 1 and 1 or math.random(1, #itemsTable)]

…or

itemsTable[math.random(1, math.max(1,#itemsTable))]

-edit-
Also, is this Firing when the items are empty?
You should probably check for that and early exit before checking the items to be destroyed

2 Likes

What happens here is not Upper limit == Lower limit,but Upper limit < Lower limit.

Your upper is smaller than the lower.

Using math.random on 2 equal numbers returns them (math.random(x,x) → x).

It seems that your items table is empty. (#itemsTable → 0)


Are you 100% certain there are things in there?

1 Like

Yes, there are items in the table, as shown in the print here:
As of total, the #itemsTable amounts to 119.


Although, after a certain threshold has passed (the number of the item’s objects less than 1 or when it is empty) it returns an error, hence why I made this post.

It seems that it runs 2 times, then at the third time, the table is empty. (as you can see 2 prints in line 38, then it fails at the third repeat in the line before 38 - 35).

I suggest you check the items folder of the third click detector.

1 Like

After a bit of documentation and trial and error, I’ve found out that indeed, the table would run even when it’s empty, expecting that there is one more object remaining. Forgive my jargon, but I think it “lags” behind the actual checking of the number of instances under the table, so I added a table.remove() function to help it keep track. Thank you everyone who gave their input! It really helps a lot.

	for i,v in ipairs(clickParts) do
		if v:IsA("ClickDetector") then
			v.MouseClick:connect(function()
				local itemsTable = v.Parent.Parent.Parent:FindFirstChild("Items"):GetChildren()
				local itemToBeDestroyed = itemsTable[math.random(1, #itemsTable)] -- ISSUE: Upper limit == Lower limit when 1 instance remains under itemsTable
				table.remove(itemsTable, 1)
				itemToBeDestroyed:Destroy()
				itemsLeft.Value -=1   
				print(#itemsTable)
			end)
		end
	end```