Lua math.random exception

Hi. I need to randomly set the X position of a part from 65.017 to 87.984, but so that it never takes a value from 72.997 to 80.025. How do I do this?

I’ve tried doing it like this, but it only takes the first argument, not considering the second possible one at all:

script.Parent.Position = Vector3.new(math.random(80.025,87.984) or math.random(65.017,72.997), 4.913, 33.025)

Any help would be appreciated

Could you provide more information of what you’re trying to do? I have a feeling you could do this more efficiently with lerping so you don’t have to make weird decimal random numbers.


I need to randomly place a line at the bottom so that it doesn’t go into the area of the square. This square is the painted decal. The line can go either to the left (in the photo) or to the right of the square

Instead, pick a random number between 0, 1 to determine which side, and then another random number for the position inside that side.

local side = math.random(0,1)
local pos = math.random(0,100)/100
local badzone = 7 -- studs wide
local goodzone = 19 -- studs wide
local position = (pos * goodzone) + (side * (badzone + goodzone)) -- end result

Also, try to never use very precise numbers like 87.984 and 65.017 inside of code, it can be confusing to look at. Try to get those values directly from the parts so it can work even if the parts move.

Taking a little bit of your idea, I made it like this. I also reduced the numbers to a hundredth

local side = math.random(1,2)
	if side == 1 then
		script.Parent.Position = Vector3.new(math.random(80.02,87.98), 4.91, 33.02)
	elseif side == 2 then
		script.Parent.Position = Vector3.new(math.random(65.01,72.99), 4.91, 33.02)
	end

Also thanks for your feedback on this post

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.