Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters

Hello guys, I am having a trouble saving data, here’s the code;

function module:SaveData()
	if self.ToSave then
		Datastore:UpdateAsync(self.Player.UserId, function(oldValue)
			local previousData = oldValue or {DataId = 0}
			if self.ToSave.DataId == previousData.DataId then
				self.ToSave.DataId += 1
				print('Success saving data :)')
				print(self.ToSave)
				return self.ToSave
			else
				return nil
			end
		end)
	end
end

It prints success saving data and then it errors… when I print self.ToSave it outputs this;

image

Seems like the trouble is here;

function module:GetData()
	local realTMP = require(script.template)
	local suc,result = pcall(Datastore.GetAsync,Datastore,self.Player.UserId)
	if (suc) then
		if (result) then
			for i,v in pairs(realTMP) do
				if (result[i] == nil) then
					result[i] = v
				end
			end
			self.ToSave = result
			return;
		end
		self.ToSave = realTMP.ToSave
	end
end

Because self.ToSave = result Is not overriding self.ToSave it’s setting self.ToSave[“ToSave”] = result
:thinking:

1 Like

This is the template


And this is when I create metatable

function module.new(plr)
	assert(plr ~= nil,"plr's nil.")
	local tmp = require(script.template)
	local self = setmetatable(tmp,module)
	self.Player = plr
	module[plr] = self
	--self:GetOwnedGamepasses()
	self:GetData()
	return self
end