How would I convert the follow [] to table.insert? Help!

Hello!
I’m currently trying to convert the follow to table.insert, since I can’t add multiple tables in []. It breaks everything.

The issue is, it doesn’t display. So I wanna stick with table.insert since I guess It’s more efficient and understandable?

I’ve tried to convert it in my attempt, but it didn’t work.

This is the code that I’m trying to convert to table.insert:

serial[tostring(cfi * children[i].PrimaryPart.CFrame)] = children[i].Name 

Anything can help! Thanks in advance.

I’ve tried

table.insert(serial, tostring(cfi * children[i].PrimaryPart.CFrame, children[i].Name)) 

But that didn’t really work out, it says It’s nil.

It says what as nil? The value? children[i]?

Also, table.insert(table, index, value) is equivalent to table[index] = value

I’m not sure what’s nil, but I will assume It’s the table
Also could you give a quicker explaination on how I could add multiple variables in a table? Thanks! :slight_smile:

What’s the error?

Multiple values? What are you trying to do? What’s the context?

Because you can either add values to a table upon defining the table,

local Table = {"foo", 1, 4}

print(Table[1]) --> foo
print(unpack(Table)) --> foo 1 4

and

local Table = {Number = 5; String = "Pet"}

print(Table.Number) --> 5
print(Table["String"]) --> Pet

or after using table.insert or table[index] = value.

you could make it as a table first then declare variables and just do some calculation maybe it would work

It’s kind of hard to explain, but I’m trying to serialize BrickColor values aslong with the PrimartPart CFrame, So I can save in the database… But it doesn’t seem to work.
This is the full code right now:

Yeah, I tried the unpacking method,

for i = 1, #children do
		local t = {
			cfi * children[i].PrimaryPart.CFrame,
			children[i].BrickColorVal.Value
		}
		
		serial[tostring({unpack(t)})] = children[i].Name 
	--	table.insert(serial, tostring(cfi * children[i].PrimaryPart.CFrame, children[i].Name)) 
	end

It just doesn’t work. :frowning: I’m confused why though.

Everything works without the BrickColor. Working code:

	for i = 1, #children do
		local t = {
			cfi * children[i].PrimaryPart.CFrame,
			children[i].BrickColorVal.Value
		}
		
		serial[tostring(cfi * children[i].PrimaryPart.CFrame)] = children[i].Name 
	--	table.insert(serial, tostring(cfi * children[i].PrimaryPart.CFrame, children[i].Name)) 
	end

But when I try to add the BrickColorVal part, it just won’t work.

Why are you trying to set a CFrame as the index, though? Could you elaborate on your use-case?

I’m not so sure why, but I made that script with the help

and it works good. But I just want it to save the BrickColorValue too, not only the CFrame.

Well, I’m still not quite sure what you’re trying to do here but you can add a table to a table like so:

local MainTable = {}
local TableValue = {String = "String", Number = 1}

-- Method 1
MainTable["IndexString"] = TableValue
-- Method 2
MainTable.IndexString = TableValue
-- Method 3
MainTable[#MainTable+1] = TableValue

I’m trying to save the BrickColorVal and CFrame to the database by serializing, basically
so that players can load their plots with the CFrame and the Brickcolors.

Anyways, I’ll try that again. See if it works.

Well, you could in that case do something along these lines:

local SaveTable = {}

for index, instance in pairs(Plot:GetDescendants()) do
	-- Assuming plot is a model containing one's furniture or similar
	
	if instance:IsA("BasePart") then
		local InstanceData = {
			CFrame = instance.CFrame; -- position and orientation
			Size = instance.Size;
			Color = instance.Color; -- more flexible than BrickColor
			Material = instance.Material;
		}

		SaveTable[#SaveTable+1] = InstanceData
	end
end

print(#SaveTable, "instances are saved")

To rebuild the plot using the saved data you’d do:

for index, InstanceData in pairs(SaveTable) do
	local Part = Instance.new("Part")
	
	for Property, Value in pairs(InstanceData) do
		Part[Property] = Value
	end 	

	Part.Parent = Plot
end

Hello,
that is very helpful!
However… I tried using some of your methods for my code, since I really don’t wanna rewrite all of it.

function Placement:Serialize()
	local serial = {}
	
	local cfi = self.CanvasPart.CFrame:inverse()
	local children = self.CanvasObjects:GetChildren()
	
	
	
	
	for i = 1, #children do
		local t = {
			tostring(
			cfi * children[i].PrimaryPart.CFrame,
				children[i].BrickColorVal.Value
				)
		}
		serial[#serial+1] = t
		serial[#serial] = children[i].Name 
		--serial[tostring(cfi * children[i].PrimaryPart.CFrame)] = children[i].Name 
		
	--	table.insert(serial, tostring(cfi * children[i].PrimaryPart.CFrame, children[i].Name)) 
	end
	
	

	
	return serial
end

Surprisingly, I can now save! But when I load the save It says " 20:56:11.289 - ReplicatedStorage.Placement:49: invalid argument #1 to ‘new’ (Vector3 expected, got number)"

Vector3 expected got number?
I’m not sure what I did wrong here…

And this is how I would load… The error is quoted.

function Placement.fromSerialization(canvasPart, data)
	local self = Placement.new(canvasPart)
	local canvasCF = canvasPart.CFrame
	data = data or {}
	
	for cf, name in pairs(data) do
		local model = furniture:FindFirstChild(name)
		if (model) then
			local components = {}
			for num in string.gmatch(cf, "[^%s,]+") do
				components[#components+1] = tonumber(num)
			end
			
			self:Place(model, canvasCF * CFrame.new(unpack(components)), false) --Is when it says Invalid argument #1 to "new" (Vector3 expected, got number)
		end
	end
	
	return self
end

Any solutions to this?

What are you thinking here? Essentially what you’re doing is overwriting the table with a string, specifically children[i].Name, which is a string.

Alright, I got save to work… But now I really have no idea how to load the BrickColoVal from the table… Any suggestions? This is how I load the building.

function Placement.fromSerialization(canvasPart, data)
	local self = Placement.new(canvasPart)
	local canvasCF = canvasPart.CFrame
	data = data or {}
	
	for cf, name in pairs(data) do
		local model = furniture:FindFirstChild(name)
		if (model) then
			local components = {}
			for num in string.gmatch(cf, "[^%s,]+") do
				components[#components+1] = tonumber(num)
			end
			
			self:Place(model, canvasCF * CFrame.new(unpack(components)), false)
			
			print"loaded dat"
		end
	end
	
	return self
end

Well, I’m serializing the table like I mentioned, thats how database loads my buildings.