Example :
First Of All, you need two things inside workspace to visualize this, A part which will move inside the defined area and a zone or the defined area
local part = workspace:FindFirstChild("MovingPart")
local Area = workspace:FindFirstChild("Area")
Now we need a function which will return the random position
local function RandomPos()
local size = Area.Size
local position = Area.Position
end
Why are we getting the position and the size??
Alright we need the size of course but why position, this is because the position gives us the center of the part which we really need.
Now we get the minimum X value and maximum X value and same for another axis (Y and Z)
local minX = position.X - size.X/2
local maxX = position.X + size.X/2
local minY = position.Y - size.Y/2
local maxY = position.Y + size.Y/2
local minZ = position.Z - size.Z/2
local maxZ = position.Z + size.Z/2
Oh my gosh what is this
Lets imagine this first think of the area as a box in 3D. The center is your start point. The edges are how far your part can roam left, right, up, down, forward, back. Every random spot is just āsomewhere between center and edgeā on each axis.
Now from that lets see here in this
local minX = position.X - size.X/2
local maxX = position.X + size.X/2
hmm, what are we doing here bro. ok so basically here in minumum X
Okay, so basically here in minX
, weāre starting at the center (which is position.X
) and then going left by half the size of the area. That gives us the left wall or the minimum X position.
And maxX
? Thatās the oppositeāwe go right from the center by half the size, giving us the right wall or maximum X.
Same thing applies to Y and Z. Weāre just defining the range of movement for the part, based on how big the box is.
Alright that concept is done now the random position.
Okay now⦠How do we get a random position?
We donāt just go like math.random(minX, maxX)
because that only gives integers, and we want smooth, floaty positions.
So hereās the better way:
We use math.random()
which gives a number between 0 and 1.
Then we multiply that with the total range (like maxX - minX
), which gives us a number between 0 and the full width.
Finally, we add that to minX
to shift it back into the real area.
So like this for X:
local randomX = math.random() * (maxX - minX) + minX
then the entire function (add a return to get the values)
local function getRandomPosition()
local size = AreaPart.Size
local position = AreaPart.Position
local minX = position.X - size.X/2
local maxX = position.X + size.X/2
local minY = position.Y - size.Y/2
local maxY = position.Y + size.Y/2
local minZ = position.Z - size.Z/2
local maxZ = position.Z + size.Z/2
-- random position maker
local randomX = math.random() * (maxX - minX) + minX
local randomY = math.random() * (maxY - minY) + minY
local randomZ = math.random() * (maxZ - minZ) + minZ
return Vector3.new(randomX, randomY, randomZ)
end
now to use this function
you have to do this yourselve but just use a while loop and call this function again and again