How can I save and load a list of string values within a folder?

What do you want to achieve?

My goal is to save all of the string values in this folder, and load it all back in exactly how it is when called.
image

What is the issue?

I understand that instances can not be saved using datastores, so I need help on how to compile all the values in the folder, and how to re-import them when called.

Just a heads up that the folder setup above is an example, in my game these folders are generated [on the server] when players do certain actions, they can add up to 10 questions, which means that within the QuestionHolder, there is a possibility of more then just the “Question1” such as “Question2”, “Question3”, and so on.

What solutions have you came up with?

From reading other posts with a similar issue to mine, I found that I need to iterate through the folder and find all of the values I’m wanting to save (StringValues). From there, I can then insert them into a table and save the table as a JSON?

From there I am stuck on if this is a viable solution, and how I would go about transforming the saved table to the folder setup as shown in the image above.

I think this is how the finished table should look like because since I want to be able to load it in as it is now, I want to be able to know which values are for what:

Quiz = {
	Questions = {
		Question1 = {
			RealAnswer = "Real answer here",
			FakeAnswer1 = "Fake answer 1 here",
			FakeAnswer2 = "Fake answer 2 here",
			FakeAnswer3 = "Fake answer 3 here"
		}
	},
	QuizName = "Quiz name here",
	QuizDescription = "Quiz description here"
}

I am new to datastores and am still learning so any help would be appreciated, thank you! :slightly_smiling_face:

there are two ways you can go about this
first way: use string manipulation to create a string out of all of your table values

second way: use HttpService:JSONEncode(table) and JSONDecode ( the easier way )

1 Like

My advice is to save it into a datastore as JSON, do these change at all?

You can load each by making a new instance within a json group.

1 Like

You do not need to JSONEncode and JSONDecode since you can just save table values in a datastore, you could make a recursive function that indexes all of the folders and saves it.

1 Like

How can I transfer a folder (like the above photo) into a dictionary formated table? Would I use table.insert or some other method to allowed values like this inside of a table from a script:

Quiz = {
	Questions = {
		Question1 = {
			RealAnswer = "Real answer here",
			FakeAnswer1 = "Fake answer 1 here",
			FakeAnswer2 = "Fake answer 2 here",
			FakeAnswer3 = "Fake answer 3 here"
		}
	},
	QuizName = "Quiz name here",
	QuizDescription = "Quiz description here"
}

Here is a quick serialiser I made

function Serialise(parent)
    local dataTable = {}
    for _, child in ipairs(parent:GetChildren()) do
        if child:IsA('Folder') then
            dataTable[child.Name] = Serialise(child)
        elseif child:IsA('ValueBase') then
            dataTable[child.Name] = child.Value
        end
    end
    return dataTable
end

You can run this and pass in the folder as the first argument and it will return a table

2 Likes

Thank you, this helped immensely! I didn’t know that much about dictionaries before this so it was a huge learning curve for me!

Also note the recursive function (if you didn’t know about that already)

I’ll have to look into that, I’ve never heard of it

It’s calling the function to itself to go down a tree until it reaches the end.

1 Like

Oh wow, I noticed that and was a bit unsure on how it worked, thanks for letting me know and what it was called!

1 Like

This works great, thank you! Do you know a way that I can interpret this dictionary and convert it to a folder? I have tried looping through the dictionary but I get stuck because, since its a dictionary, there are tables within tables. I found online that type(table) will return “table” if the table value is indeed a table, and now that @ElusiveEpix told me about recursive functions I think I can use that in this case too. Any help would be greatly appreciated, you’ve already helped so much! Thank you!

You write a deserializer for this to reverse the process. Here’s what it might look like:

local function Deserialise(table)
	local dataFolder = Instance.new("Folder")
	for key, child in pairs(table) do
		if typeof(child) == "table" then
			local childFolder = Deserialise(child)
			childFolder.Name = key
			childFolder.Parent = dataFolder
		elseif typeof(child) == "number" then
			local childValue = Instance.new("NumberValue")
			childValue.Value = child
			childValue.Name = key
			childValue.Parent = dataFolder
		elseif typeof(child) == "string" then
			local childValue = Instance.new("StringValue")
			childValue.Value = child
			childValue.Name = key
			childValue.Parent = dataFolder
		elseif typeof(child) == "boolean" then
			local childValue = Instance.new("BoolValue")
			childValue.Value = child
			childValue.Name = key
			childValue.Parent = dataFolder
		end
	end
	return dataFolder
end

(The typeof function is a more precise version of the type function that interprets Roblox instances correctly)

4 Likes

Wow, you are so helpful! Thanks again so much for all your help, I really appreciate your time. Happy New Years!

1 Like