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!
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
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.
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.
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
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
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