A while back, I made a resource which ran a for loop through a specified target and appended the IDs to a table corresponding to its class and it did pretty well for such a small resource, but I’m wondering if there’s any optimizations or formatting to make it better. Please leave feedback!
Code:
local ModuleDictionary = {}
local DecalDictionary = {}
local TextureDictionary = {}
local SoundDictionary = {}
local MeshDictionary = {}
local AnimationDictionary = {}
local VideoDictionary = {}
local ImageLabelDictionary = {}
local ImageButtonDictionary = {}
local ShirtDictionary = {}
local PantsDictionary = {}
--//functions
local function AddToTable(Target)
for index = 1, #Target do
local Cases = {
["Decal"] = function ()
table.insert(DecalDictionary, Target[index].Texture)
end,
["Texture"] = function ()
table.insert(TextureDictionary, Target[index].Texture)
end,
["Sound"] = function ()
table.insert(SoundDictionary, Target[index].SoundId)
end,
["Mesh"] = function ()
table.insert(MeshDictionary, Target[index].MeshId)
end,
["Animation"] = function ()
table.insert(AnimationDictionary, Target[index].AnimationId)
end,
["ImageLabel"] = function ()
table.insert(ImageLabelDictionary, Target[index].Image)
end,
["ImageButton"] = function ()
table.insert(ImageButtonDictionary, Target[index].Image)
end,
["VideoFrame"] = function ()
table.insert(VideoDictionary, Target[index].Video)
end,
["Shirt"] = function ()
table.insert(ShirtDictionary, Target[index].ShirtTemplate)
end,
["Pants"] = function ()
table.insert(PantsDictionary, Target[index].PantsTemplate)
end,
}
--print(Target[index], Target[index].ClassName)
local success, err = pcall(function()
Cases[Target[index].ClassName]()
end)
if not success then
warn("Instance is not supported. Expected a downloadable or supported asset, got "..tostring(Target[index].ClassName))
end
end
end
function ModuleDictionary:GetDictionary(Target: Instance, Descendants: boolean?, DeleteTarget: boolean?, Sort: boolean?, Reverse: boolean?, Print: boolean?)
Descendants = Descendants or false
DeleteTarget = DeleteTarget or false
Sort = Sort or true
Reverse = Reverse or true
Print = Print or false
if Descendants == false then
AddToTable(Target:GetChildren())
elseif Descendants == true then
AddToTable(Target:GetDescendants())
elseif typeof(Descendants) ~= "boolean" then
error("GetDictionary expects \"Descendants\" to be a boolean, got "..tostring(Descendants), 2)
end
local function ReverseTable(ID1, ID2)
return ID1 > ID2
end
if Reverse == false and Sort == true then
table.sort(DecalDictionary)
table.sort(TextureDictionary)
table.sort(SoundDictionary)
table.sort(MeshDictionary)
table.sort(AnimationDictionary)
table.sort(ImageLabelDictionary)
table.sort(ImageButtonDictionary)
table.sort(VideoDictionary)
table.sort(ShirtDictionary)
table.sort(PantsDictionary)
elseif Reverse == true and Sort == true then
table.sort(DecalDictionary, ReverseTable)
table.sort(TextureDictionary, ReverseTable)
table.sort(SoundDictionary, ReverseTable)
table.sort(MeshDictionary, ReverseTable)
table.sort(AnimationDictionary, ReverseTable)
table.sort(ImageLabelDictionary, ReverseTable)
table.sort(ImageButtonDictionary, ReverseTable)
table.sort(VideoDictionary, ReverseTable)
table.sort(ShirtDictionary, ReverseTable)
table.sort(PantsDictionary, ReverseTable)
elseif typeof(Reverse) ~= "boolean" then
error("GetDictionary expects \"Reverse\" to be a boolean, got "..tostring(Reverse), 2)
elseif typeof(Sort) ~= "boolean" then
error("GetDictionary expects \"Sort\" to be a boolean, got "..tostring(Sort), 2)
end
--//inserting
ModuleDictionary.DecalDictionary = DecalDictionary
ModuleDictionary.TextureDictionary = TextureDictionary
ModuleDictionary.SoundDictionary = SoundDictionary
ModuleDictionary.MeshDictionary = MeshDictionary
ModuleDictionary.AnimationDictionary = AnimationDictionary
ModuleDictionary.ImageLabelDictionary = ImageLabelDictionary
ModuleDictionary.ImageButtonDictionary = ImageButtonDictionary
ModuleDictionary.VideoDictionary = VideoDictionary
ModuleDictionary.ShirtDictionary = ShirtDictionary
ModuleDictionary.PantsDictionary = PantsDictionary
if DeleteTarget == true then
Target:Destroy()
elseif typeof(DeleteTarget) ~= "boolean" then
error("GetDictionary expects \"DeleteTarget\" to be a boolean or nil, got "..tostring(DeleteTarget), 2)
end
if Print == true then
print("Decals:", DecalDictionary)
print("Textures:", TextureDictionary)
print("Sounds:", SoundDictionary)
print("Meshes:", MeshDictionary)
print("Animations:", AnimationDictionary)
print("Image Labels:", ImageLabelDictionary)
print("Image Buttons:", ImageButtonDictionary)
print("Videos:", VideoDictionary)
print("Shirts:", ShirtDictionary)
print("Pants:", PantsDictionary)
elseif typeof(Print) ~= "boolean" then
error("GetDictionary expects \"Print\" to be a boolean or nil, got "..tostring(Print), 2)
end
end
--//memory freeing functions
function ModuleDictionary:ClearTable(Table: string)
if typeof(Table) == "string" then
local cases = {
["Decal"] = function()
table.clear(DecalDictionary)
ModuleDictionary.DecalDictionary = nil
end;
["Texture"] = function()
table.clear(TextureDictionary)
ModuleDictionary.TextureDictionary = nil
end;
["Sound"] = function()
table.clear(SoundDictionary)
ModuleDictionary.SoundDictionary = nil
end;
["Mesh"] = function()
table.clear(MeshDictionary)
ModuleDictionary.MeshDictionary = nil
end;
["Animation"] = function()
table.clear(AnimationDictionary)
ModuleDictionary.AnimationDictionary = nil
end;
["ImageLabel"] = function()
table.clear(ImageLabelDictionary)
ModuleDictionary.ImageLabelDictionary = nil
end;
["ImageButton"] = function()
table.clear(ImageButtonDictionary)
ModuleDictionary.ImageButtonDictionary = nil
end;
["Video"] = function()
table.clear(VideoDictionary)
ModuleDictionary.VideoDictionary = nil
end;
["Shirt"] = function()
table.clear(ShirtDictionary)
ModuleDictionary.ShirtDictionary = nil
end;
["Pants"] = function()
table.clear(PantsDictionary)
ModuleDictionary.PantsDictionary = nil
end;
}
local success, err = pcall(function()
cases[Table]()
end)
if not success then
error("ClearTable expects the string to be a supported asset, got "..Table, 2)
end
else
error("ClearTable expects \"Table\" to be a string, got ".. tostring(Table), 2)
end
end
function ModuleDictionary:ClearAllTables() --//no arguments needed, and will only clear asset dictionary tables.
table.clear(DecalDictionary)
table.clear(TextureDictionary)
table.clear(SoundDictionary)
table.clear(MeshDictionary)
table.clear(AnimationDictionary)
table.clear(ImageLabelDictionary)
table.clear(ImageButtonDictionary)
table.clear(ShirtDictionary)
table.clear(PantsDictionary)
ModuleDictionary.DecalDictionary = nil
ModuleDictionary.TextureDictionary = nil
ModuleDictionary.SoundDictionary = nil
ModuleDictionary.MeshDictionary = nil
ModuleDictionary.AnimationDictionary = nil
ModuleDictionary.ImageLabelDictionary = nil
ModuleDictionary.ImageButtonDictionary = nil
ModuleDictionary.ShirtDictionary = nil
ModuleDictionary.PantsDictionary = nil
end
return ModuleDictionary
Here’s the documentation for it if you’re confused:
The code is probably messy lol