Attempted to index Nil with "GetChildren()"

I’m currently trying to access a folder with all of it’s children to try and put it inside of a for loop, problem is that for some reason it says it’s nil even though I switch to the server side and see contents inside of it? Is the issue that the folder is empty or am I making another mistake with this?

Here’s the script I have issues with

game.ReplicatedStorage.Events.BuyMilFac.OnServerEvent:Connect(function(player, Link)
	if Link ~= "MilFacSubtract" then
		local Insert = game.Workspace.CapturePoints:FindFirstChild(Link)
		local Factory = game.ReplicatedStorage.Clones.MilFac:GetChildren()
		local Positions = Insert.MilFacs.Locations:GetChildren()
		
		
		for i,v in pairs(Positions) do
			if i == 1 then
			local FinalPosition = v.Position
			local FactoryNew = game.ReplicatedStorage.Clones.MilFac:Clone()
			local CurrentCFrame = game.ReplicatedStorage.Clones.MilFac.PrimaryPart.CFrame
			local rX, rY, rZ = CurrentCFrame:ToOrientation()
			local NewCFrame = CFrame.new(FinalPosition) * CFrame.fromOrientation(rX, rY, rZ)
			local num = 0
			local add = 1
				
			FactoryNew.Name = v.Name
			FactoryNew.Parent = Insert.MilFacs.BoughtFactories
			FactoryNew:SetPrimaryPartCFrame(NewCFrame)
			local destroy = Insert.MilFacs.Locations:FindFirstChild(FactoryNew.Name)
			destroy:Destroy()
			end
		end
		
		
	else
		local Insert = game.Workspace.CapturePoints:FindFirstChild(Link)
		local Location = game.ReplicatedStorage.Clones.Part:Clone()
		
		for i,v in pairs(Insert.MilFacs:GetChildren()) do -- Works up until this point --
			print (v.Name)
			Location.Parent = Insert.MilFacs.Locations
			Location.Name = v.Name
		end
	end
end)

It just tells me “Attempt to index nil with MilFacs”

1 Like

I suppose your ‘local Insert = game.Workspace.CapturePoints:FindFirstChild(Link)’ is looking for a child with the name “MilFacSubtract” in ‘CapturePoints’, which isn’t present in the setup you’ve posted, unless you’re spawning it in with another script?

Print out your Link and the variables you use after casting them to see if any are Nil.

Gotta script defensively, even on the server.

I realize now what you mean, thanks, this solved it.

Aight, glad I could be of help, good luck with it :slight_smile:

game.ReplicatedStorage.Events.BuyMilFac.OnServerEvent:Connect(function(player, Link)
	if Link ~= "MilFacSubtract" then
		local Insert = game.Workspace.CapturePoints:FindFirstChild(Link)
		local Factory = game.ReplicatedStorage.Clones.MilFac:GetChildren()
		local Positions = Insert.MilFacs.Locations:GetChildren()

		for i,v in pairs(Positions) do
			if i == 1 then
				local FinalPosition = v.Position
				local FactoryNew = game.ReplicatedStorage.Clones.MilFac:Clone()
				local CurrentCFrame = game.ReplicatedStorage.Clones.MilFac.PrimaryPart.CFrame
				local rX, rY, rZ = CurrentCFrame:ToOrientation()
				local NewCFrame = CFrame.new(FinalPosition) * CFrame.fromOrientation(rX, rY, rZ)
				local num = 0
				local add = 1
				FactoryNew.Name = v.Name
				FactoryNew.Parent = Insert.MilFacs.BoughtFactories
				FactoryNew:SetPrimaryPartCFrame(NewCFrame)
				local destroy = Insert.MilFacs.Locations:FindFirstChild(FactoryNew.Name)
				destroy:Destroy()
			end
		end
	else
		local Insert = game.Workspace.CapturePoints:FindFirstChild(Link)
		local Location = game.ReplicatedStorage.Clones.Part:Clone()
		if Insert.MilFacs then
			for i,v in pairs(Insert.MilFacs:GetChildren()) do -- Works up until this point --
				Location.Parent = Insert.MilFacs.Locations
				Location.Name = v.Name
			end
		end
	end
end)

Insert.Milfacs must’ve been nil at that point in the code for that to occur, you just need to define Insert.Milfacs correctly to fix that.