Improving my random generated obby with premade stages

hello, I’m trying to make my randomly generated obby without the same stages but I don’t have any idea on how to make it.

here’s what I’ve done so far and works perfectly fine but sometimes the stages will repeat and I want to avoid that.

for i = 0, 10 do
	local amountofstages = stages:GetChildren()
	local x = math.random(1, #amountofstages)
	local newstage = stages[x]:Clone()
	newstage.Parent = obbyfolder
	newstage:SetPrimaryPartCFrame(initialpoint.CFrame + (initialpoint.CFrame.RightVector*-99*i)) 
end

There are tons of different ways to do this. One simple solution is to create a table with all of the stages inside, then sort it randomly. When picking a stage, go in order and repeat from the beginning once you reach the end of the table.

Some pseudo code:

local stages = stages:GetChildren()
for i = #stages, 2, -1 do
	local rand = math.random(i)
	stages[i], stages[rand] = stages[rand], stages[i]
end

-- later

local currentIndex = 1
for i = 0, 10 do
	local newstage = stages[currentIndex]:Clone()
	newstage.Parent = obbyfolder
	newstage:SetPrimaryPartCFrame(initialpoint.CFrame + (initialpoint.CFrame.RightVector*-99*i))
	
	currentIndex = currentIndex + 1
	if currentIndex  > #stages then
		currentIndex = 1
	end
1 Like