GetChildren not working - Server Script

I’m making a script in which when the server first loads it’ll create a bunch of cellVars folders equal to the amount of children inside the “Cells” folder located inside the workspace.

The issue here is that when I use GetChildren() the children table is empty “{}”

--Requires
local req = require(script:WaitForChild("CellContainer"))
-- Numbers
local cellCap = 20
local i = 0
-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
--Directories
local cells = game.Workspace:WaitForChild("Cells")

function cellGen()
local children = game.Workspace.Cells:GetChildren()
	print("Running cellCheck")

local i = 0
	print("Created var")
	print(children)

		while i < #children do 
	print("Running loop")
		wait(0.01)
local child = children[i]

local CellVars = Instance.new("Folder")
local conString = tostring(child)
		CellVars.Name = conString
		CellVars.Parent = ServerStorage.Cells
i = i + 1
		print("Cell: "..i)
end
end

 cellGen()

Additionally, if you wish you can comment on my formatting. I’m attempting to improve my formatting because A: My scripts become messy for me to read and that’s tedious to deal with as well as B: It’s easier on your eyes when you’re aiming to help me.

1 Like

You formatting is great just when you pasted it over to devforum it kinda died…

1 Like
--Requires

local req = require(script:WaitForChild("CellContainer"))

-- Numbers

local cellCap = 20

local i = 0

-- Services

local Players = game:GetService("Players")

local ServerStorage = game:GetService("ServerStorage")

--Directories

local cells = game.Workspace:WaitForChild("Cells")

function cellGen()

local children = game.Workspace.Cells:GetChildren()

print("Running cellCheck")

for i, item in pairs(children) do

local child = item

local CellVars = Instance.new("Folder")

CellVars.Name = child.Name

CellVars.Parent = ServerStorage.Cells

print("Cell: "..i)

end

end

cellGen()

There are a couple of problems I spotted in your code. Firstly, you can’t “tostring” an instance, secondly I don’t recommend using the variation of looping through children that you do. There is a much easier method (which I used above). Good Luck!

1 Like

This doesn’t resolve my current issue though(though I appreciate the advice with i, ipairs as it is a lot neater and a lot less tedious to call - a lifesaver to be honest.)

Underneath the line
print("Running cellCheck")
I put
Print(children)
and the output prints an empty Children table and doesn’t run the loop:

Thank you, that’s my new formatting which is why I asked for opinions on it. When I say new, I mean my first formatting. I didn’t really format before and I was always getting stressed out trying to make sense of the disjointed jumble that was my scripts.

1 Like

Can you show me the explorer? (It’s probably cuz the game hasn’t loaded in yet idk)

1 Like

Ah, thank you! It was a rather ridiculous mistake, I had a model named ‘Cells’ which I didn’t realise as I thought I only added a folder(my problem is likely that I went to the model, created it, went to work on something else, forgot it existed, realised a folder would be better and then created a folder which led to two ‘Cells’)

In other words, it was returning the empty contents of the ‘Cells’ mode, not the abundant contents of the ‘Cells’ folder.

1 Like