Question about naming tables/dictionaries (HORRIBLY WRITTEN, SIMPLIFIED IN EDIT)

hello! the thing i’m trying to do right now is saving data from a collection of folders as tables, and i want the table to have the name of the folder

to clear up, here’s an example of what i’m trying to do

Folder called “Folder1” with values inside it, and a folder called “G” with the same value structure as Folder1, just different values

What i want to do is save both of these into a table, as tables, while keeping their name.

So for example, the table should look like this after everything’s been properly added

t = {Folder1,G}

my issue is with how i would make the tables be named after the folder, if it helps, i’m running this through a GetChildren loop

i tried to word this better, but couldn’t, hope everyone can understand, and, could anyone help me with this?

EDIT: So, i relized just how terribly i had worded this, pretty much what i’m trying to say is “how can i name a variable after a folder”

code sample below

for i, child in ipairs(children) do
		if child:IsA("Folder") then
			-- makes a table with the name of the child
 			-- then adds the table we just made to another table defined previously, for example, t
		end
	end

So you want to set the key of the entry to the name of the property/child and the value to be the value?

So I guess your structure would be

ClassName: Name

Folder: MyFolder
-- BoolValue: MyValue
-- Folder: MyOtherFolder
---- StringValue: MyString

into

local t = {
  BoolValue = true,
  MyOtherFolder = {
    MyString = "asdf"
  }
}

Here is an example of how I would do it.

function tableFromFolder(folder: Folder) -- If you want ANY instance, replace with "Instance"
	local folderTable = {}
	-- Loop through each child
	for _, child in folder:GetChildren() do
		-- If it is a folder, use recursion to do the same and
		-- convert its children into another table and use that
		if child:IsA("Folder") then
			folderTable[child.Name] = tableFromFolder(child)
		-- If it is a value (such as StringValue or BoolValue), store its value directly
		elseif child:IsA("ValueBase") then
			folderTable[child.Name] = child.Value
		end
	end
	-- Return the finished result
	return folderTable
end

local t = tableFromFolder(script.Folder)

You can specifically set the key of a table to the key of the folder like this

tbl[folder.Name] = {folder1 values}
tbl[g.Name] = {g values}

In a get children loop it would look something like this:

local t = {}
t[folder.Name] = {}

for _, item in folder:GetChildren() do
    t[folder.Name][item.Name] = item.Value
end

@majdTRM @dragonvale5555 i added a small edit at the end of the post which greatly simplifies my question, i haven’t exactly tried any of your examples yet, but i wanted to know if they fit my new explanation of what i’m trying to do, to see if i really messed up with writing that badly or if it’s alright, sorry for the inconvenience :’)

Oh, that would be quite simple.

Get the namw via folder.Name, set the value to whatever you want

local t = {}
t[folder.Name] = 1234

i’m not sure if this will do what i intend to happen, pretty much, it should get a folder, make a table that has the folder’s name, then add the values that are inside the folder into the table, and then adding the folder table into a previously defined table, like “t”

so pretty much the end result should be like so
(Folder1 and G would be the two folders that we got in the for loop)
t = {Folder1,G}
the “t” table would have as many indexes as there are children, and they would all have the same name as their respective folder

I don’t know if this made it even more confusing, i hope not, i’m not very sure of what i’m supposed to ask

Well I’m not sure what

Means.

Are Folder1 and G supposed to be Instances? Or tables?

Could you share a screenshot of the explorer of what the inout data is, and also share the code for a table which should be your output? I mean like with proper keys and everything.

You mentioned you wanted to “Name” a table. You can’t name tables but you can set an entry in another table with they key being the name and the value being something else.

local t = {
  ["Folder1"] = {},
  ["G"] = {}
}

Sorry if this is very confusing.

So are you wanting two seperate tables inside of one master table like this:

-masterTable
 -Folder1
  -values
 -G
  -values

or all of them in one table like this:

-masterTable
 -values

Folder1 and G are example names for the folders that the script would get with GetChildren()

Their names would be given to tables, so that they can be searched for by name when loading data using table.find()

These tables would then be put inside one bigger table, where they will be found using the folder’s name

first example, as this would be to search a group of values that correspond to individual items

In that case, what @majdTRM said in the first reply should work.
image
image

1 Like

imagen_2023-02-10_194356132
Here is a rough example of how the folder structure would be, the resulting “master table” should be something like this

{Folder1,G}
and you would be able to access the values inside the “mini-tables”

Folder1.ExampleValue1
G.ExampleValue2

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