Help with :GetChildren() error

Hello, I’ve been experimenting with scripting these days and today I came across one error that’s triggering me.

wait(1)

local setting = require(game.ServerScriptService.Kiosk)
local kioskfolder = game.ServerStorage:FindFirstChild(setting.Name)
local category = kioskfolder:FindFirstChild(script.Parent.Name)

for _, v in ipairs(category:GetChildren()) do
    local clone = script.Parent.Template:Clone()
    clone.Parent = script.Parent
    clone.Visible = true
    clone.ItemName.Text = v.Name
    clone.Name = v.Name
end

The script is supposed to get all children from a folder inside of another folder although it doesn’t seem to work the way I tried to do it.

This is the error I’m getting.

Any kind of help is appreciated!

You are getting things too early, you must wait before getting them because they haven’t loaded yet!

local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

-- Always yield when requiring modules
local setting = require(ServerScriptService:WaitForChild("Kiosk"))

-- This folder might have not loaded yet, you could check if so just by printing this variable
local kioskfolder = ServerStorage:WaitForChild(setting.Name)
local category = kioskfolder:WaitForChild(script.Parent.Name)

for _, v in ipairs(category:GetChildren()) do
    local clone = script.Parent.Template:Clone()
    clone.Parent = script.Parent
    clone.Visible = true
    clone.ItemName.Text = v.Name
    clone.Name = v.Name
end

Hello, thank you for replying! It shows that there’s an infinite yield possible on the console, when I use that code.

1 Like

Have you checked that you placed the “setting” module script in the right place? Btw I recommend placing module scripts in the server storage instead of the server script service :smiley:

You didn’t name the module script correctly, and there might be a space at the end of the name. Also, you don’t need to use WaitForChild on the server unless you’re dealing with someone’s character just getting added into the game.

What do you mean with naming it correctly?

Why then, the setting variable which holds the required module is named “setting” but the module is named “Kiosk”? (I think you mismatched the names which is the problem)

local setting = require(ServerScriptService:WaitForChild("setting"))

The module script is named after “Kiosk”.

Then move it to the server storage and change its variable patch accordingly

Are you sure that script.Parent.Name is a VAILD member of kiosfolder??

Of course it is, it’s supposed to look for the folder that has the same name as the frame.

That didn’t do anything. Still the same error.

Is this a Server Script or a Local Script?

If it’s a Local Script then add the kiosfolder inside ReplicatedStorage instead and Change the code to this (Also parent the Kisok module to ReplicatedStorage).

wait(1)

local setting = require(game.ReplicatedStorage.Kiosk)
local kioskfolder = game.ReplicatedStorage:FindFirstChild(setting.Name)
local category = kioskfolder:FindFirstChild(script.Parent.Name)

for _, v in ipairs(category:GetChildren()) do
    local clone = script.Parent.Template:Clone()
    clone.Parent = script.Parent
    clone.Visible = true
    clone.ItemName.Text = v.Name
    clone.Name = v.Name
end

There is no child that has the name of the script parents. Check if it even exists in the explorer.

He said it does.

do you mind sharing the setting module script and screenshot of explorer kioskfolder?

image

ok so i guess is that because the script.Parent.Name is ItemsTemplate according to this screenshot

so that’s why the category is nil and error because script can’t find ItemsTemplate in Kiosk folder because it doesn’t exist

image

you need to change ItemsTemplate’s name to something else or just make the script find the specific folder like

local category = kioskfolder:FindFirstChild("Coffee")

I’ve done this before but I want this to be modular, therefore it should work with the folder named with any name.

You need to change the ItemsTemplate name to the corresponding name for the script to work.

1 Like