How to move part in random positions

Hi,

I wanted to move a Part in random positions. The platform that I made for the part to move has 2 points on the other 2 corners.

image

(Upper point is PointA, the lower point is PointB, and the blue part in the middle is the Part.)

I get a error like invalid argument #2 to 'random' (interval is empty) and I know what it means but anyway to fix it?

Script:

local pointA = game.Workspace:WaitForChild("PointA")
local pointB = game.Workspace:WaitForChild("PointB")

local main = game.Workspace:WaitForChild("Main")

while wait(1) do
	local aX = pointA.Position.X
	local bX = pointB.Position.X
	local aZ = pointA.Position.Z 
	local bZ = pointB.Position.Z 
	
	main.Position = Vector3.new(math.random(aX, bX), 2, math.random(aZ, bZ))
end 

Thanks in advance

What’s the solution?
Any alternative?

Math.random(min, max) if min is bigger than max then it’ll cause that error.

I know, is there a way to avoid that error?

Yes instead of using 2 points or 2 parts you can just

Part.Position = Zone.Position + Vector3.new(math.random(-Zone.Size.X/2, Zone.Size.X/2), 2, math.random(-Zone.Size.Z/2, Zone.Size.Z/2))

PS: Edited it cause forgot to add the division.

The script works and it didn’t cause any errors but it was offset by the platform.

I edited it try it again, It shouldn’t go off platform now, unless you already tried it and I assume your current problem is the Part somewhat being outside the platform?

Example:
image

It’s completely outside the platform
I haven’t tested the edited script I’ll let you know if there are any issues

Alright the edited one shouldn’t completely go outside the platform.

I’m fine with it not inside the platform

Incase you get PTSD about it, this will completely stop that and make sure its 100% Inside the zone, thought if the Part is bigger than the zone then I can’t guarantee that 100%.

Part.Position = Zone.Position + Vector3.new(math.random((-Zone.Size.X/2) + (Part.Size.X/2), (Zone.Size.X/2) - (Part.Size.X/2)), 2, math.random((-Zone.Size.Z/2) + (Part.Size.Z/2), (Zone.Size.Z/2) - (Part.Size.Z/2)))
1 Like

I got what I wanted it doesn’t go outside the platform thanks

1 Like