Attempt to index number with number error

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.

stageNumber is a number. newframe is also a number

1 Like

I made that fix, but now it is getting the error: ServerScriptService.StageLoader:20: attempt to index nil with 'FindFirstChild'

if it’s on that same line then whatever you replaced stageNumber with (probably stageList?) has no value stored at newframe

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)

Place file:
GameNotWorking.rbxl (31.2 KB)

Not able to replicate what you’re describing. It loads in the Template stage, then errors here.

newframe is a number; FindFirstChild uses string name to lookup children. newframe would have to be ‘Template’ for this to not error here.

1 Like

Oh I see, I forgot to change the name to the number stage it is. My bad! :sweat_smile: