Invalid argument #2 to "random" - But list is fine and OK

Hello! This is an issue that’s pretty much stumping me. I’m trying to get something to randomize a Vector3, but this part in the function for it is stumping me. The table NewValues prints OK, but the random function won’t accept it. It’s made from 2 vector3s.

local newValues = {
		x1 = max.X,
		x2 = min.X,
		y1 = max.Y,
		y2 = min.Y,
		z1 = max.Z,
		z2 = min.Z
	}
print(newValues)
local newRandoms = {
	x = math.random(newValues.x1,newValues.x2), -- erroring line
	y = math.random(newValues.y1,newValues.y2),
	z = math.random(newValues.z1,newValues.z2)
}
1 Like

I believe the problem is that you’ve got the arguments in the wrong order. For math.random() to work, the smaller value has to come before the larger when defining the arguments. Switching the values around should work:

x = math.random(newValues.x2, newValues.x1)

(You should also do this for the y and z values)

2 Likes

Sadly, this doesn’t work. It gives me the same error:

  21:15:24.874  ServerStorage.Modules.Vector3Module:21: invalid argument #2 to 'random' (interval is empty)  -  Server - Vector3Module:21

(Note- Line 21 is the line I marked as the erroring line)

The interval is empty error only occurs when the second value is larger than the first.

What are the values of min.X and max.X? Assuming they vary, maybe print their values before making the random calculation to make sure that x2 is less than x1.

1 Like

I print it and it’s normal, 2nd smaller then first. ???