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.
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