Converting a dictionary to instances of folders and values

I’m looking to create something like this but in reverse. The name of this post says it all.

Here is what I have currently.

function Util.ConvertDictToInstances(tableRef, parent)
	local function recurse(subTable, subParent)
		for index, value in pairs(subTable) do
			
			if typeof(value) == "table" then
				local holder = Instance.new("Folder", subParent)
				holder.Name = index
				recurse(value, holder)
			else
				--Hmmm...
			end
			
		end
	end
	
	recurse(tableRef, parent)
end

So far, it generates the folders correctly but I’m not sure how to get started when handling values. The only values I’ll be handling are numbers, ints, bools, and enums.

Do you only care about the value when going through the dictionary? Or do you use the key in any way?

I use the index/key to name the value or folder.

But the value determines the data type correct?

Indeed.

local dict = {KeyIsHere = "ThisIsTheValue"}

Ok and final question, are you creating objects such as IntValue, BooleanValues, etc…

That’s the plan. For things like enums StringValues could be used. If a dynamic system were to be implemented then that would probably need to be hard-coded in.

local DataTypes =
{
number = "IntValue",
boolean = "BoolValue"
};

if (DataTypes[typeof(1)]) then
    print(DataTypes[typeof(1)]);
end  

Have a dictionary of datatype returns e.g: print(typeof(true)) and link them to an object. Then, instantiate that object by doing Instance.new([DictionaryValue])

So if you do typeof(1), it returns “number”

Then, check if it exists in the dictionary, and create its instance off of the ObjectString linked to it. It’s very scaleable.

Do you plan on using value objects as the instances in the folder? If so then you could do something like:

local PossibleValueTypes = {
	string = "StringValue",
	number = "NumberValue"
	---etc. (can add more if you need)
}

function Util.ConvertDictToInstances(tableRef, parent)
	local function recurse(subTable, subParent)
		for index, value in pairs(subTable) do
			
			if typeof(value) == "table" then
				local holder = Instance.new("Folder")
				holder.Name = index
				holder.Parent = subParent
				recurse(value, holder)
			else
				local typ = PossibleValueTypes[typeof(value)]
				if (typ ~= nil) then
					local valueinst = Instance.new(typ)
					valueinst.Name = index
					valueinst.Value = value
					valueinst.Parent = subParent
				end
			end
			
		end
	end
	
	recurse(tableRef, parent)
end
1 Like

Well, I Beat you to it :wink:

Great minds think alike, cheers!

2 Likes

As @Exeplex stated you both have the same solution, haha. Cool solution.
I’m gonna give the answer to Exeplex because he got it first lol.

1 Like

Ironic how you’re using a dictionary of object values, to convert a dictionary back into object values. Almost poetic :slight_smile:

1 Like