I have a datastore script that saves everything within a certain folder.
This is the folder:
Everything within this folder is for saving. Our script has it so that if a folder is inputted, it will check if the folder has a folder until it finds a Value object. The folders will be stored within a table. This is the Save function:
local function Save(folder, data) -- Input the folder to save. data is a table
for _, v in pairs(folder:GetChildren()) do
if v:IsA('Folder') then
print(v.Name, data)
Save(v, data[folder.Name])
else
print(v.Name, data, folder.Name)
data[folder.Name][v.Name] = v.Value
end
end
end
In this case, ‘data’ is a table in which everything is supposed to be stored. Unfortunately, this is the error that appears, along with the print statement.
The erroring line of code is when the second Save() within the Save() is called.
if v:IsA('Folder') then
print(v.Name, data)
Save(v, data[folder.Name]) -- This is erroring
I have tried multiple solutions, but none of them work. The first print statement in the error message shows LandVehicles and an empty table. The second print shows Camaro and nil. Nil should instead have a table like so:
Make sure that data[folder.Name] is not nil . If it is nil , then you should initialize it as an empty table before calling Save(v, data[folder.Name]) . You can do this by adding a line of code like data[folder.Name] = {} before calling Save(v, data[folder.Name]) .
Make sure that folder.Name is a valid key in the data table. If folder.Name is not a valid key, then data[folder.Name] will be nil . You should check if folder.Name is a valid key in the data table before calling Save(v, data[folder.Name]) .
Make sure that v is actually a Folder object. If v is not a Folder object, then Save(v, data[folder.Name]) will not be called and the error will not occur. You can add some debugging code to print the type of v to see if it is actually a Folder object.
Make sure that v has children. If v does not have any children, then the for loop in the Save function will not execute and Save(v, data[folder.Name]) will not be called. You can add some debugging code to print the number of children v has to see if this is the issue.
I tried out solution 2. Now the script gets past the recurring stage but then gets stuck in the value setting stage, saying that the table does not exist…?
I tried using step 2 for the else statement as well. Now, there are no errors and it is printing the data saved accurately. The only fault is that the final table saved only contains leaderstats folder and not all the other folders.
The code right now:
local function Save(folder : Instance, data : {})
print(data)
for _, v in pairs(folder:GetChildren()) do
if v:IsA('Folder') then
data[folder.Name] = {}
print(v.Name, data)
Save(v, data[folder.Name])
else
data[folder.Name] = {}
print(v.Name, v.Value, data[folder.Name], folder.Name)
data[folder.Name][v.Name] = v.Value
end
end
end
Solved this problem. Turns out, we just thought about it the wrong way. data[folder.Name] just needs to be equal to the function. Changed the function so it returns the dictionary.
local function ReturnDictionary(folder : Instance)
local Dictionary = {}
for _, v in ipairs(folder:GetChildren()) do
if v:IsA('Folder') then
Dictionary[v.Name] = ReturnDictionary(v)
elseif v:IsA("ValueBase") or v.ClassName:match("Value") then
Dictionary[v.Name] = v.Value
end
end
return Dictionary
end