Can't store dictionary in datastore

I’m trying to make a model serializer/deserializer that saves, but it’s not letting me save dictionaries.

local ss = {}
ss.__index = ss

local dss = game:GetService("DataStoreService"):GetDataStore("Serializer")

function ss.new(model : Model, save : boolean?)
	local self = setmetatable({model = model}, ss)
	if save ~= nil and save == true then self.save = true end
	return self
end

function ss:serialize()
	local objs = self.objects
	local serialized_objs = {}
	for _,v in pairs(self.model:GetChildren()) do
		serialized_objs[v.Name] = {
			ClassName = v.ClassName,
			Name = v.Name,
			Size = v.Size,
			Position = v.Position,
			Transparency = v.Transparency,
			Parent = v.Parent
		}
	end
	self.serialized_objs = serialized_objs
	if self.save then dss:SetAsync("Serializer", self.serialized_objs) end --where the error occurs
	--print(self.serialized_objs)
end


function ss:deserialize()
	assert(dss:GetAsync("Serializer"), "Nothing to deserialize")
	assert(self.save, "Save mode not enabled")
	print(dss:GetAsync("Serializer"))
	--[[
	local m = Instance.new("Model")
	for _,v in pairs(dss:GetAsync("Serializer")) do
		m.Name = v.Parent.Name
		local p = Instance.new(v.ClassName)
		p.Name = v.Name
		p.Size = v.Size
		p.Position = v.Position
		p.Transparency = v.Transparency
		p.Parent = game.Workspace[v.Parent.Name]
	end
	]]
end

return ss

You’re not serializing the Vector3s in the Size and Position Properties, and you’re not serializing the instance in the Parent property.

2 Likes

if im correct you just need to use jsonencode to turn the dictionary into a string then save it and when you want to get the data then just use jsondecode to turn it back into a dictionary

hope that helps