Can't save array to datastores

I am making a furniture placement system, and I need to save the item and the CFrame. With one key, I can save the the item but I can’t save the CFrame. I tried using a dictionary but turns out you can’t. It says you have to save them as strings or numbers so I converted the CFrame into numbers. But I saved the numbers in a table and that table inside a bigger table so when I GetAsync I will get the bigger table and it’s easier to find the corrosponding CFrame.

Now the problem is that I have an error.
13:11:38.720 - ServerScriptService.SaveAndLoadPlot:62: 104: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.
The error is in the 62nd line, but I don’t know what 104 means. It says I can’t store arrays but I have used arrays in Datastore for a long time. Does this mean I can’t save tables inside tables?

4 Likes

Can you share your code?

Looks like you’re encoding a string here that Roblox is identifying as an invalid UTF-8 keycode. A sample of what you’re trying to save would also be useful.

1 Like
local function encode(cf) 
    return {cf:components()}
end

game.Players.PlayerRemoving:Connect(function(plr)
	local usedGrid
	for i, v in ipairs(workspace:GetChildren()) do
		if v.Name == 'Grid' and v.Owner.Value == plr.Name then
			usedGrid = v
		end
	end
	local f = workspace:WaitForChild(plr.Name)
	local t = {}
	local t2 = {}
	for i, v in ipairs (f:GetChildren()) do
		table.insert(t, v)
	end
	for i, v in ipairs (f:GetChildren()) do
	t2[1] = encode(v:GetPrimaryPartCFrame())
	end
	local yes, no = pcall(function()
		Store:SetAsync(plr.UserId, t)
		Store:SetAsync('cframe'..plr.UserId, t2)
	end)
	if not yes then error(no) end
	f:Destroy()
	usedGrid.Owner.Value = ''
end)

I might be doing something wrong.

Datastores cannot save instances, so the array in which you have the parts of the plr’s character, t, will error.

5 Likes

Adding onto this, you’ll want to create two functions to serialise and deserialise these Instances. Best way to do this is to turn the Instances into dictionaries of properties - just remember to encode any Roblox datatypes.

ie.

local function Serialise(Object)
    return {
        Class = Object.ClassName;
        Properties = { Name = Object.Name } --// Add more if needbe
    }
end

local function Deserialise(Data)
    local Object = Instance.new(Data.Class);

    for Name, Value in pairs(Data.Properties) do
        Object[Name] = Value;
        --// Ensure you add deserialising logic here
    end
end
1 Like

I just found out I’m trying to save Instances. Edited it, and now it’s working smoothly.