I am getting the attempt to index number with number error when I try to run this code:
local storage = game:GetService("ServerStorage")
local obbyStart = storage.ObbyStart
local stageFolder = storage.Stages
local stageList = stageFolder:GetChildren()
local event = game:GetService("ReplicatedStorage").GameEvents.LoadNewStages
event.Event:Connect(function(length)
if tonumber(length) then
local loadNumber = tonumber(length)
for loaded = 1, loadNumber, 1 do
local choice = math.random(1, #stageList)
local stageToLoad = stageList[choice]
print("Loading stage with name " ..stageToLoad.Name)
local clone = stageToLoad:Clone()
clone.Name = loaded
clone.Parent = workspace.LoadedStages
local stageNumber = #workspace.LoadedStages:GetChildren()
local newframe = stageNumber - 1
if stageNumber ~= 0 then
clone:SetPrimaryPartCFrame(stageNumber[newframe]:FindFirstChild("End").CFrame)
else
clone:SetPrimaryPartCFrame(obbyStart.CFrame)
end
end
end
end)
This code is used to load stages, but it only loads one.
I have changed that, but now it loads all of the stages inside of eachother and all upside down.
Current script:
local storage = game:GetService("ServerStorage")
local obbyStart = storage.ObbyStart
local stageFolder = storage.Stages
local stageList = stageFolder:GetChildren()
local event = game:GetService("ReplicatedStorage").GameEvents.LoadNewStages
event.Event:Connect(function(length)
if tonumber(length) then
local loadNumber = tonumber(length)
for loaded = 1, loadNumber, 1 do
local choice = math.random(1, #stageList)
local stageToLoad = stageList[choice]
print("Loading stage with name " ..stageToLoad.Name)
local clone = stageToLoad:Clone()
clone.Name = loaded
clone.Parent = workspace.LoadedStages
local stageNumber = #workspace.LoadedStages:GetChildren()
local newframe = stageNumber - 1
if stageNumber ~= 0 then
clone:SetPrimaryPartCFrame(workspace.LoadedStages:FindFirstChild(newframe):FindFirstChild("End").CFrame)
else
clone:SetPrimaryPartCFrame(obbyStart.CFrame)
end
end
end
end)