Need help making this generate a random level

Hello! I hope you are all doing well. I am creating an endless obby, however, this will not choose a level. it works when I make it 1 level and not 2:

level = {
	"Land0",
	"OtherLevel"
}
local repStor = game:GetService("ReplicatedStorage")
local Land = repStor:WaitForChild("ObbyLevels"):FindFirstChild(math.random(#level))

local new_land = Land:Clone()
new_land.Parent = game.Workspace
new_land.Name = "Land1"

maxland = 5

for i = 1, maxland do --Doing for loop for testing
	Land = repStor:WaitForChild("ObbyLevels"):FindFirstChild(math.random(#level))
	print("Working", i)
	local oldLand = game.Workspace:FindFirstChild("Land"..i)
	local new_new_land = Land:Clone()
	new_new_land.Name = "Land"..i + 1
	new_new_land.Parent = game.Workspace
	new_new_land:SetPrimaryPartCFrame(oldLand.ThePrimaryPart.CFrame * CFrame.new(94, 0, 0))
	wait(5) -- To prevent crashing... Had to learn that the hard way. 

end

Here is how ReplicatedStorage is layed out out:
image

Here is the error im getting:
image

If you can help, please let me know. Thanks, WE

1 Like

Quick little update:
I tried :GetChildren() but it didn’t work. First 14 lines:

level = {
	"Land0",
	"OtherLevel"
}
local repStor = game:GetService("ReplicatedStorage")

local levelFolder = repStor:WaitForChild("ObbyLevels")
local levels = levelFolder:GetChildren()

local Land = repStor:WaitForChild("ObbyLevels"):FindFirstChild(math.random(#levels))

local new_land = Land:Clone()
new_land.Parent = game.Workspace
new_land.Name = "Land1"

The other lines are the same.

Same error but on different line:
image

I think your are supposed to do

:FindFirstChild(level[math.random(1,#levels)])
1 Like