Error In Script


I have created a script which in theory would randomly generate different chests at random positions, but my script currently seems to be incorrect. The error is:

Workspace.Chest Region.ChestRandomizer:17: bad argument #2 (interval is empty)
(line is the first math.random)

The script is as follows:

local Point1 = script.Parent:WaitForChild("Point1")
local Point2 = script.Parent:WaitForChild("Point2")
local CommonChest = game.Workspace.CommonChest
local Pos1 = Point1.Position
local Pos2 = Point2.Position
local X1 = Pos1.X
local Y1 = Pos1.Y
local Z1 = Pos1.Z
local X2 = Pos2.X
local Y2 = Pos2.Y
local Z2 = Pos2.Z
while true do
	--Get random coordinates
	local xRand = math.random(X1,X2)
	local yRand = math.random(Y1,Y2)
	local zRand = math.random(Z1,Z2)
	if math.random(1,10) <= 5 then
		CommonClone = CommonChest:Clone()
		CommonClone.Transparency = 0
		CommonClone.Anchored = false
		CommonClone.Position.X = xRand
		CommonClone.Position.Y = yRand
		CommonClone.Position.Z = zRand
		print("Successfully created a Common Chest!")
	if math.random(5,10) <= 7 then
			print("Successfully created a Rare Chest!")
			if math.random(8,10) <= 9 then
				print("Successfully created an Epic Chest!")
				print ("Successfully created a legendary chest!")
	local waittime = math.random(1,5)
	--Destroy Clones Before Next Loop Starts

Any help will be greatly appreciated.

try replacing those random statements with:

local xRand = math.random(math.min(X1, X2), math.max(X1, X2))
local YRand = math.random(math.min(Y1, Y2), math.max(Y1, Y2))
local ZRand = math.random(math.min(Z1, Z2), math.max(Z1, Z2))

If you’re using two arguments for math.random, then the second argument has to be a larger number than the first. They may have just been in the wrong order.


Thank you for your help. It didn’t cross my mind to use math.min and math.max. Thanks! This will also help because it is soft coded.

Your upper limit is lower than your lower limit.

I think you most likely did it backwards. Just remember, your max number first, than your lower number second. (sent this before the other guy but internet cutout. )

It is always good to look up the error code. I found a solution in 1 min.

1 Like

Yep, and the solution the guy above provided will make this 10x easier since I won’t have to compare numbers and if I change the position of the area.