Need help on how to create this

Hello.

Is there any way to turn a table into a folder using typeof ? Like I have an entire table that has values and booleans and tables in it, I want to create a filter where it loops through the table and create the inatances value and also a folder for each table that are inside the main table, and parent each value in their table just like how they were parented in the main table.

For example:

local module = {


	MainTable = {
		
		Coins = 0,
		Diamoinds = 0,
		
		Exp = 0,
		Level = 1,

        Boosts = {
          ["Rare"),
          ["Mythic"),
        }
		
	}
	
}

return module

I want this MainTable to turn into a folder, and whatever inside it will also turn to its instance value, and the tables inside turn into a folder.

Any help is appreciated.

Here’s a way I’d do it,

function convertToFolder(tab, parent)
    local fold = Instance.new("Folder", parent)
    for i,val in pairs(tab) do
       local  t = typeof(val)
       if t=="boolean" then
           t = "bool"
       end

       local inst = string.gsub(t, "^%l", function(c) return string.upper(c) end).."Value" ---- will be used when creating object for value

       if t =="table" then
           convertToFolder(val, fold) ---- Table in Table --> Folder in Folder
       else
           local obj = Instance.new(inst, fold)
           obj.Value = v 
           --obj.Name = i
       end
    end
end

P.S: I would recommend changing the indexes in your module to strings so the name is assigned when creating the instances.

2 Likes

you can create a function and insert a for in do loop, after that you just check all the values ans if its a number than it creates an intvalue, if its a string than a string value, etc… and if the type is a table, create a new folder and call the function again. I would recomend giving it a path of a folder.

If you don’t understand, than i can write you the code, but that might take a while.

Does it parent the values that are inside the table inside the MainTable to their table ? I got an idea of how to get this work, thanks.

Not really what I’m asking, I’ve tried this and made a module to handle all type of values, only wanted to have more organized script with less lines to make a filter going through whatever is being added in that Table will turn into a its typeof value instance and will be parented to where it is in the table just like the “Boosts” table in my post ^

Thanks for trying to help.

1 Like

The second parameter of Instance.new() is the parent of the instance which I set to the folder, and the folder is created from the given table.

Oh, I didn’t see the second parameter, thanks.

1 Like

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