Saving Folder of Values?

Hey Devs,

In my game I use a folder of BoolValues that keep track of when the player purchases something using their in game currency. Once they purchase something, the value is set to true; and when the value changes, the Shop GUI changes so that they are not able to purchase that Item again.

I have been struggling to figure out how, even if, I can save that folder of values, so that whenever the player leaves, it saves, and whenever they rejoin, their shop GUI will be the same as when they left.

For reference, here are the folders that I am trying to Save:

This folder manages the Player’s Inventory, and if there is a value in one of these folder, then that item will appear within their Inventory GUI.
image

This folder manages the Player’s Shop GUI; if the BoolValue is set to true then they will not be able to Purchase that Item again.
image

Basically I am looking for how I would be able to save these each of these folders, with each of their subfolders, and each of the values all at once.

If anyone has some experience with saving anything other than stats, then please provide me with any information that could help me figure this out. Thanks!

local SaveTable = {
	Effects = {
		'effect1',
		'effect2'
	},
	Animations = {
		'animation1',
		'animation2'
	}
}

Key:SetAsync(UserId, SaveTable)

From there on, you can figure out the rest once you see how it’s set up. Good luck!

1 Like

I typically write a utility function for this called folderToDictionary which is, as the name implies, intended to pack the hierarchy of a folder into a dictionary. It’s recursive so it can handle arbitrary depths worth of values. Good for serialisation.

local function folderToDictionary(folder)
    local dictionary = {}

    for _, object in ipairs(folder:GetChildren()) do
        if object:IsA("Folder") then
            dictionary[object.Name] = folderToDictionary(object)
        elseif object:IsA("ValueBase") or object.ClassName:match("Value") then
            dictionary[object.Name] = object.Value
        end
    end

    return dictionary
end

I do find myself troubled when trying to unpack a dictionary back into a folder structure though, quite a number of times. I don’t have a reverse utility function for that but someone else might. Worst and lazy case scenario, I use paths that I can break up with string.split to help me unpack where all results of the split except the last are used as a relative hierarchical path (the last being a key-value pair’s key).

4 Likes

I believe that I have been able to save the values that are within the folders without getting any errors.

However, I am not sure of how to load the values back into the correct folder. This is currently how my loading code is setup (quiet messy at the moment, sorry):

if stored then
		local animations = player.InventoryValues:WaitForChild("Animations")
		local effects = player.InventoryValues:WaitForChild("Effects")
		local phones = player.InventoryValues:WaitForChild("Phones")
		local robux = player.InventoryValues:WaitForChild("Robux")
		local sanim = player.ShopValues:WaitForChild("Animations")
		local seffects = player.ShopValues:WaitForChild("Effects")
		local sphones = player.ShopValues:WaitForChild("Phones")
		local srobux = player.ShopValues:WaitForChild("Robux")
		likes.Value = stored[1]
		refreshes.Value = stored[2]
		followers.Value = stored[3]
		posts.Value = stored[4]
		likemodifier.Value = stored[5]
		followermodifier.Value = stored[6]
		animations = stored[7]
		effects = stored[8]
		phones = stored[9]
		robux = stored[10]
		sanim = stored[11]
		seffects = stored[12]
		sphones = stored[13]
		srobux = stored[14]

The folders start at 7, and each stored is a table of the values within that folder. I get no errors when saving, but as I said I cannot seem to load these tables into the folder.

More specifically, I do not know how to unpack a table to begin with. I believe that I cannot just set the store to the folder; but have not been able to find anything about how to unpack.