Converting a folder of objects into a dictionary

I’m messing around with datastore and trying to make a script that converts a folder of values into a dictionary of dictionaries.
image
:arrow_down:
image

Have you figured out any smart ways to do that? Doing this the other way around would be easy, but creating dictionaries isn’t my piece of cake. I tried going through them all manually with for loops but it feels kind of… Clunky, and limits the amount of children to be added.

And how would I store the value of the dogs hair for an example as its already assigned to start a new dictionary? Add it as first object in the dictionary or include it in the key as:

Dog(Value) = {
		Ear = {},
		Hair(value) = {
			Grease = 1000,
			Color = Color3.fromRGB(255, 24, 217)
		},
		Teeth = {},
	}

?

Thank you in advance :thinking:

I wasn’t sure on the value types/how many layers of instances you intend to have but here.

local folder = workspace:WaitForChild("Folder") --change workspace
local save = {}

for _, pet in ipairs(workspace:GetChildren()) do
	if pet.Value then --check for pet
		save[pet.Name] = {} --create new array for pet
		for i, feature in ipairs(pet:GetChildren()) do
			if feature.Value then --check for feature
				save[pet.Name][feature.Name] = {} --create new array for feature
				for _, trait in ipairs(feature:GetChildren()) do
					if trait.Value then --check for trait
						save[pet.Name][feature.Name][trait.Name] = trait.Value --assign trait value to trait item in array
					end
				end
			end
		end
	end
end

I have an old utility function for this (and it’s on a thread that you could’ve searched for!):

I’ll try this out in a moment, gracias :thinking:

local folder = --Path to ur folder
local dictaOfValues = {}

for _, v in pairs(folder:GetChildren()) do 
     if v:IsA("ValueBase") then do
          folder[v.Name] = {}
     end
     for _, w in pairs(v:GetChildren()) do
          if v:IsA("ValueBase") then do
               folder[v.Name] = w
          end
     end
end

Tried pretty much what you did here before I posted this, but I need it to create a deeper list of dictionaries.This only goes down by two steps, I’d need something to iterate through the whole folder 'till the deepest end. :sad:

Actually I can use this method to suit my needs with a little editing on the folder. Thank you!

You’d just be overriding the empty array with the value stored inside “w”, as opposed to inserting the value stored by “w” into the array.

I see ur correct, but since thesolution was found i didnt give it a second thot, thnx 4 pointing it out :smiley: