Random position for Y axis gives error

I want to make clouds that are positioned in a region, but the Y axis keeps erroring, which says:
invalid argument #2 to 'random' (interval is empty)

The other axis works perfectly, but it’s only the Y axis that causes the error to pop up/

local randomPosition = Vector3.new(
		math.random((region.Position.X - region.Size.X / 2) + cloud.Size.X / 2, (region.Position.X + region.Size.X / 2) - cloud.Size.X / 2),
		math.random((region.Position.Y - region.Size.Y / 2) + cloud.Size.Y / 2, (region.Position.Y + region.Size.Y / 2) - cloud.Size.Y / 2),
		math.random((region.Position.Z - region.Size.Z / 2) + cloud.Size.Z / 2, (region.Position.Z + region.Size.Z / 2) - cloud.Size.Z / 2)
	)

did u try to print out the math.random statements and your randomPosition variable?

it could perhaps be a problem where math.random(min, max) → the min parameter could be bigger than the max parameter. eg. math.random(50, 45) hence causing it to error.

this post suggests that u could also use Random.new instead if u still have recurring problems. :slight_smile:

Your right! I figured that since the regions Y axis is smaller than the clouds Y, the min and max values would be reversed.

I just removed the + cloud.Size.Y / 2 and - cloud.Size.Y / 2 part so I wouldn’t need to make the region bigger.

1 Like