How to make parts spawn in a region?

Title is all that’s needed. -----

Have you searched google… Anyways, something you can do is place 2 parts on opposite corners of the area you want to spawn the parts in (place one part on top left and one on bottom right) then all you have to do is this:

local topLeft = workspace.TopLeftPart
local bottomRight = workspace.BottomRightPart
local ranX = math.random(topLeft.Position.X, bottomRight.Position.X)
local ranZ = math.random(topLeft.Position.Z, bottomRight.Position.Z)

local part = Instance.new("Part")
part.Position = Vector3.new(ranX, 0, ranZ)
part.Parent = workspace
2 Likes
local rndObj = Random.new()

local function RandomSpawn(part : BasePart, range : {number}) : (BasePart, {number}) -> ()
	part.Position = Vector3.new(rndObj:NextNumber(table.unpack(range, 1, 2)), rndObj:NextNumber(table.unpack(range, 3, 4)), rndObj:NextNumber(table.unpack(range, 5, 6)))
end

local part : BasePart = Instance.new("Part")
RandomSpawn(part, {-2, 2, -2, 2, -2, 2})
print(part.Position) --1.9371631145477295, 0.09097576886415482, 0.2920038402080536
1 Like