# How To Make A Randomized Obby-Generator Regardless Of Level Size

So I made a sort of randomized obby-generator.

I wanna make it so that regardless of size, it can properly position the next obby far enough from another level that it doesn’t intersect with one another.

I got some progress, but it’s still kind of janky.

Here’s the code:

``````local levels: number = 10
local current: {Model} = {}
local set: {Model} = script.Set:GetChildren()
local start: CFrame = CFrame.new(16.05, 0.5, 9.1)

for i = 1,levels do
local clone: Model = set[math.random(1,#set)]:Clone()
clone.Parent = workspace.Folder

table.insert(current, clone)

local orientation, size: Vector3 = clone:GetBoundingBox()
local lastIndex: Model = current[i - 1]

if lastIndex then
clone:SetPrimaryPartCFrame(lastIndex:GetPivot() + Vector3.new(size.X + 5,0,0))
else
clone:SetPrimaryPartCFrame(start)
end
end
``````

You have to move it forward by half the length of the previous level + half the length of the current level.

Maybe this diagram helps visualize it. If `A = size.X` and `B = lastIndexBoundingBoxSize.X`, then the difference between the two center points is `A/2 + B/2`.

``````+---A---+------B-------+
|       |              |
|   +   |      +       |
|       |              |
+-------+--------------+
``````
1 Like

Alternatively, you can have connection points for each obby section that just need to line up. You can set the primary part to Connection A, then move the position to the same position as Connection B of the last section.

3 Likes

Thank you so much! I got it working.

Here’s the code for those wondering:

``````local levels: number = 10
local current: {Model} = {}
local set: {Model} = script.Set:GetChildren()
local start: CFrame = CFrame.new(16.05, 0.5, 9.1)

for i = 1,levels do
local clone: Model = set[math.random(1,#set)]:Clone()
clone.Parent = workspace.Folder

table.insert(current, clone)

local _, size1: Vector3 = clone:GetBoundingBox()
local lastIndex: Model = current[i - 1]

if lastIndex then
local _, size2: Vector3 = lastIndex:GetBoundingBox()

clone:SetPrimaryPartCFrame(lastIndex:GetPivot() + Vector3.new((size2.X / 2 + size1.X / 2) + 5,0,0))
else
clone:SetPrimaryPartCFrame(start)
end