Cannot store Dictionary in data store

My system of saving data currently is:
→ Convert Data folder to a table
→ Save the table to datastore

local function ConvertFolderToTable(folder:Folder)
	local Data = {}
	
	for i, DataFolder in folder:GetChildren() do
		Data[DataFolder.Name] = {}
		
		for j, att in DataFolder:GetAttributes() do
			Data[DataFolder.Name][j] = DataFolder:GetAttribute(j)
		end
	end
	
	return Data
end

local function ConvertTableToFolder(data: {})
	local Folder = Instance.new("Folder")
	local DefaultData = game.ServerScriptService.Serverside.DefaultData 

	for name, subdata in data do
		local DataFolder = Instance.new("Folder")
		DataFolder.Name = name
		DataFolder.Parent = Folder


		for attributeName, value in subdata do
			DataFolder:SetAttribute(attributeName, value)
		end


		local DefaultSubDataFolder = DefaultData:FindFirstChild(name)

		if DefaultSubDataFolder then



			for Attribute, Value in DefaultSubDataFolder:GetAttributes() do
				if not DataFolder:GetAttribute(Attribute) then
					DataFolder:SetAttribute(Attribute, Value)
				end
			end
		end
	end


	for _, defaultFolder in DefaultData:GetChildren() do
		if not Folder:FindFirstChild(defaultFolder.Name) then
			local n = defaultFolder:Clone()
			n.Parent = Folder
		end
	end

	return Folder
end

Players.PlayerAdded:Connect(function(plr)
	local playerData
	
	local success, err = pcall(function()
		playerData = PlayerDataStore:GetAsync(tostring(plr.UserId))
	end)
	
	
	if not success then
		plr:Kick("Data failed to load!", err)
	end

	if not playerData then
		playerData = ConvertFolderToTable(script.DefaultData)
	end

	-- Create player's data folder
	local Folder = ConvertTableToFolder(playerData)
	Folder.Name = "Data"
	Folder.Parent = plr

	plr.CharacterAdded:Connect(function(char)
		-- irrelevant stuff for this topic
	end)
end)

Players.PlayerRemoving:Connect(function(plr)
	local PlayerDataFolder = plr:FindFirstChild("Data")

	if PlayerDataFolder then
		local PlayerData = ConvertFolderToTable(PlayerDataFolder)

		local success, err = pcall(function()
			PlayerDataStore:SetAsync(plr.UserId, PlayerData)
		end)

		if not success then
			warn("Failed to save data for player:", plr.Name, "-", err)
		end
	end
end)

My attributes only have strings, numbers, booleans and color3s in them.

Whenever i exit the game the output returns
DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. API: SetAsync, Data Store: TestingDataStore

How do i make this work? This is the first time i’ve dabbled with datastores.

Color3s can’t be stored in a data store. You’ll need to serialise them into a table.

local function serialise(colour: Color3): {[string]: number}
    return {
        ["R"] = colour.R,
        ["G"] = colour.G,
        ["B"] = colour.B
    }
end

local function deserialise(colour: {[string]: number}): Color3
    return Color3.new(colour.R, colour.G, colour.B)
end
1 Like