Database folder sorting help

Hi, I really need help with sorting folders for database. I’m using GetDescendants so its bit harder to make then using just 1 layer of folders.

Issue?? there is lot of them. maybe 1 fix can break many thing that are currently working.

Soulutions?? I have tried some of them like finding if that folder exists and if it does then script place it into that folder and some simple ones.

image

What I get:
image

What I need:
image

Code:

for i,v in pairs(PD:GetDescendants()) do
		if v:IsA("Folder") and client:WaitForChild("PlayerData") then
			local folder = Instance.new("Folder")
			folder.Name = v.Name
			folder.Parent = PlayerDataFolder
			if v.Parent.Name ~= "PlayerData" and PlayerDataFolder:WaitForChild(v.Parent.Name)then
				for i, p in pairs(PlayerDataFolder:GetDescendants()) do
					if p:WaitForChild(v.Parent.Name) then
						folder.Parent = p[v.Parent.Name]
					end
				end
			end
		end
	end

Don’t mind about values on picture I have perfect sorting for them

1 Like

Have a variable before the loop that sorts the folders, that each time will store the previous folder, and each iteration, set the current folder’s parent to the previous folder.
Try implementing this in your script.

local previous --the variable
for i, v in pairs(the folders) do
    if previous then --we add this because at the first iteration, the previous wouldn't be set
        v.Parent = previous
    else
        v.Parent = player data or whatever is the main repositry
    end
    previous = v
end

would it be better if I just clone main folder?? its going to be pre sorted tho

Well, yeah, clonning the main folder is a much efficient idea. You have to sort it first of course as you said.

2 Likes