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
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)
@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 :’)
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
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.