Folder To Table

image
image
how do you do it like this

1 Like

I guess like this

local Services = {}

for _, Service in pairs(ServicesFolder:GetChildren()) do
      if Service:IsA("Folder") then
            Services[Service.Name] = {}

            for _, Service2 in pairs(Service:GetChildren()) do
                  Services[Service.Name][Service2.Name] = Service2
            end
      end
end

It is correct to a certain extent, but it does not work if there is a folder inside the dataservice.

What is content of folder inside the dataservice?
I made a quick-test of what i’ve written and if contents of folder is another module scripts, then it should work.

local Services = {}

for _, Service in pairs(ServicesFolder:GetChildren()) do
	if Service:IsA("Folder") then
		Services[Service.Name] = {}

		for _, Service2 in pairs(Service:GetDescendants()) do
			if Service2:IsA("ModuleScript") then
				Services[Service.Name][Service2.Name] = Service2
			end
		end
	end
end
local function loopFolder(folder, tbl)
	for _, inst in ipairs(folder:GetChildren()) do
		if inst:IsA("Folder") then
			loopFolder(inst, tbl[inst.Name])
		else
			tbl[inst.Name] = inst
		end
	end
	return tbl
end

local Services = loopFolder(ServicesFolder, {})

This reminds me of how AGF loads scripts in. Not sure if you need the ipairs, and I haven’t tested it but it should work.

edit: I’m stupid and forgot the return

Hi there, So doing this is actually really easy with recursion and checking if it is a module script OR if it is a folder.

here’s an example

local function FormatToTable(ReadingFrom)
	local CurrentTable = {}
	for i,q in pairs(ReadingFrom:GetChildren()) do
		if q:IsA("Folder") then
			CurrentTable = FormatToTable(CurrentTable[q.Name],q)
		else
			CurrentTable[q.Name] = q
		end
	end
	return CurrentTable
end

so if i did this correctly you should be able to get a table of all the objects inside of the DataServices Folder

looks like we answered the question at the same time

1 Like
local function ToTable(Object)
    local Table = Object:GetChildren()
    for i, Obj in ipairs(Table) do
        if Obj:IsA("Folder") then
            Table[Obj.Name] = ToTable(Obj)
        else
            Table[Obj.Name] = Obj
        end
        Table[i] = nil
    end
    return Table
end

Thats actually just a rewriting 09glich post with another variables.

i was typing from phone and couldnt see posts from earlier lol

1 Like
local ServiceLoader = {}

local ServicesFolder = script.Parent.Services
ServiceLoader.Services = {}
local function loopFolder(folder, tbl)
	for _, inst in ipairs(folder:GetChildren()) do
		if inst:IsA("Folder") then
			tbl[inst.Name] = {}
			loopFolder(inst, tbl[inst.Name])
		elseif inst:IsA("ModuleScript") then
			print(tbl)
			tbl[inst.Name] = inst
		end
	end
	return tbl
end

local Services = loopFolder(ServicesFolder, ServiceLoader.Services)

return ServiceLoader

Sorry for replying a little late. some people forgot to create a table this is the last and correct working version thank you all.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.