I’m trying to create a function which turns a folder in to an array.
The goal of the function is to turn the descendants of a folder in to an array, so I can save it to a datastore. I don’t understand why the function returns nil when I try to use it.
The code
function CreateTable(folder)
local tab = {}
local children = folder:GetChildren()
for i,v in pairs(children) do
if Savables[v.ClassName] then
if v.ClassName == “Folder” then
tab = CreateTable(v)
tab[“N”] = v.Name
tab[“C”] = v.ClassName
tab[tostring(i)] = tab
else
if v.Value then
tab[“V”] = v.Value
end
tab[“N”] = v.Name
tab[“C”] = v.ClassName
tab[tostring(i)] = tab
end
end
end
return tab
end
Simplified the function and changed some minor details.
function CreateTable(folder)
local tab = {}
local children = folder:GetChildren()
for i, v in pairs(children) do
if Savables[v.ClassName] then
if v:IsA("Folder") then
tab = CreateTable(v)
else
if v.Value then
tab["V"] = v.Value
end
end
tab["N"] = v.Name
tab["C"] = v.ClassName
tab[tostring(i)] = tab
end
end
return tab
end
Seems like you are overwriting the keys over and over again, and not creating new entries after each incursion and so on. The function above is not a solution quite yet, as it is just for easier reading.
To solve the issue, there seems to be conflicting variables, you could instead rewrite the variable to a non-conflicting one.