How to save a dictionary with Datastore2?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I need to save a folder of values.
  2. What is the issue? Include screenshots / videos if possible!
    I cannot figure out how to save a dictionary, I have tried everything on the dev forum but I just dont understand it. I am using a folder to dictionary function and saving it on bind to close but all values just stay set to true (each value is a boolean)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried lots of different solutions but none of them work properly for me.

Here is my current code, right now it is setting every single value to true.

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") or object:IsA("Trail") then
				if object:IsA("ValueBase") or object.ClassName:match("Value") then
				dictionary[object.Name] = object.Value
				elseif object:IsA("Trail") then
					dictionary[object.Name] = false
					end
		end
	end

	return dictionary
end

local undefinedtStore = DataStore2("undefinedt", player)
	local undefinedvals = undefinedtStore:Get(false)
	for i2,v2 in pairs(game.ReplicatedStorage.Trails:GetChildren()) do
		if not trailinventory:FindFirstChild(v2.Name) then
			local trailnew = Instance.new("BoolValue")
			trailnew.Name = v2.Name
			trailnew.Value = undefinedtStore:Get(false)
			trailnew.Parent = trailinventory
		end
	end
	game:BindToClose(function()
		local undefinedtrails = folderToDictionary(trailinventory)
		undefinedtStore:Set(undefinedtrails)
	end)

Please respond with answers for Datastore2, not roblox’s default Datastore (I know datastore2 uses roblox’s default datastore)

2 Likes
local DataStore2 = require(module)
local DefaultTable = {
    [1] = 123,
    [2] = false,
    [3] = "Test"
}

game.Players.PlayerAdded:Connect(function(player)
    local Data = DataStore2.Combine("MainKey", "DictData")

    local DictData = DataStore2("DictData", player)

    local function UpdateTable(UpdatedValue)
        local dict = DictData:GetTable(UpdatedValue)
    end

    UpdateTable(DefaultTable)
    DictData:OnUpdate(UpdateTable)
end)

Using Modular tables instead of inside the script works to and is more efficient as you can require it in every script instead of copy & pasting the table into every script.

Instead of using a folder you can use a table and it’ll work fine as well. If you want to save the folder you’ll probably need to use :GetChildren() or for loops and save it that way (that’ll probably be harder)

LINKS
DataStore2 API Documentation
AlvinBlox Tutorial

I am confused on how to access these stored values, how can I get them inside of the folder? I have a folder named trailinventory in the playerobject and I need the values inside of trailinventory to be saved so I can physically retrieve them through other scripts.

You want to get the data store name and do :GetTable() and inside the parentheses plug in the default table. Then you have your data table.

How can I set an individual value with this? I have the folder of values how do I cycle through and set them all. Currently I am doing this but it doesnt save:

for i2,v2 in pairs(game.ReplicatedStorage.Trails:GetChildren()) do
	if not trailinventory:FindFirstChild(v2.Name) then
		local trailnew = Instance.new("BoolValue")
		trailnew.Name = v2.Name
		trailnew.Value = undefinedtStore:Get(DefaultTable)
		trailnew.Parent = trailinventory
	else
		table.insert(ignorednames,v2.Name)
	end
end