Object data serialization error

Hello, I was creating object data serialization so I can make all of those objects at another time with a script using a table, but I actually forgot how to make “sub”(?)-tables using table.insert().

local fold = workspace.tr
local temp = {
	
}
local desc = fold:GetDescendants()

for i = 1, math.floor(#desc/2) do
	local j = #desc - i + 1
	desc[i], desc[j] = desc[j], desc[i]
end

for _,v in pairs(desc) do
	table.insert(temp,v.Name)
	if v:IsA("Part") or v:IsA("MeshPart") then
		table.insert(temp[v.Name], v.Position) -- error is here

This might sound like a dumb question and I know I’ve asked it before, I just don’t know what to search and everything I’ve tried so far is off-topic from what I’m doing. I guess I want temp to look something like this:

{
Part1 = {
   ...
   },
Part2 = {
   ...
   }
}

Based on this, you are going to have v.Name link to a table, not just a single item. So you’ll need to make it into an empty table if it doesn’t exist already before inserting:

	if v:IsA("Part") or v:IsA("MeshPart") then
		temp[v.Name] = temp[v.Name] or {}
		table.insert(temp[v.Name], v.Position)

I would be careful using a part’s name to serialize though because names might not be unique.

1 Like

It worked for the original problem of this topic, but do you know why when I try to JSONEncode the temp table, everything is either nil, 0 or false? It’s not the same if i don’t encode it but here’s the difference:
Screenshot_338

It probably doesn’t encode EnumItems, Vector3s, CFrames and things of that sort

2 Likes

Hm that makes sense, shall I just make everything a string?

Yea, or you could possible store them as another table inside the main table and just get them later

local vect3 = Vector3.new(4, 5, 6)
local vector3 = { x = vect3.X, y = vect3.Y, z = vect3.Z }

local cf = CFrame.new(5, 5, 5)
local cframe = {cf:GetComponents()}
-- you could probably do something like this for serializing, but saving them as strings is easier
local vect3 = table[2] -- example
local newVector3 = Vector3.new(vect3.x, vect3.y, vect3.z) -- loading it into a vector3
1 Like