Saving plot structures

  1. What do you want to achieve? I would like to learn the simpliest way to save plot structures as well as their colour, material, vector3 position, rotation, size, transparency. How would I get to do this?

I have heard of something called serializing but that seems too complicated so I’m just wondering if anybody could explain a way to me as simple as possible with an example as well? Thank you in advance.

I recommend this tutorial by @EgoMoose, it’s very well put together and easy to understand. He shows you how to serialize and save plot structures as well, although only their positions.

If you want to save colour, you’ll have to store the colour along with the structure data, which means you’ll have to modify the serialising functions.

Here’s an example of how you could do that.

function Placement:Serialize()
	local serial = {}

	local cfi = self.CanvasPart.CFrame:inverse()
	local children = self.CanvasObjects:GetChildren()

	local function SerializePartColor(Color) -- Packs the RGB values of a Color3 object into an array
		return { Color.R, Color.G, Color.B }
	end

	local function GetPartColors(SearchInstance) -- Function for getting part colors. Recursive.
		local Colors = {
			IsChildrenData = true,
		}
		for _, Part in SearchInstance:GetChildren() do -- Loop through parts of the SearchInstance.

			if #Part:GetChildren() > 0 then -- If the part may have other parts inside it.

				local PartChildrenColors = GetPartColors(Part) -- Calls this function, but with the part as the SearchInstance.
				PartChildrenColors._MyColor = if Part:IsA('BasePart')
					then { IsColor = true, RGB = SerializePartColor(Part.Color) }
					else nil

				Colors[Part.Name] = PartChildrenColors

			elseif Part:IsA('BasePart') then -- Just a regular part
				Colors[Part.Name] = { IsColor = true, RGB = SerializePartColor(Part.Color) }
			end
		end
		return Colors
	end

	-- key = object space cframe string
	-- value = object name
	for _, PlotStructure in children do
		local objectSpaceCF = cfi * PlotStructure.PrimaryPart.CFrame


        -- Take note, we've completely changed data structure from the original one.
		serial[PlotStructure.Name] = {
			objectSpaceCF = tostring(objectSpaceCF),
			partColors = GetPartColors(PlotStructure),
		}
	end

	return serial
end

function Placement.fromSerialization(canvasPart, data)
	local self = Placement.new(canvasPart)
	local canvasCF = canvasPart.CFrame

	-- if data is nil or empty then this constructor is the same as .new()
	data = data or {}

	local function ColorParts(ParentInstance, ColorData) -- Function for painting part colors from data. Recursive.
		for _, Part in ParentInstance:GetChildren() do
			local MyColorData = ColorData[Part.Name]

			if not MyColorData then -- Not a Part or not something with Parts in it.
				continue
			end

			if Part:IsA('BasePart') then -- Is a part

                -- Unpack the serialised RGB values into a color3 value.
				Part.Color = Color3.fromRGB(
					table.unpack(
						if MyColorData.IsColor
							then MyColorData.RGB
							elseif MyColorData.IsChildrenData then MyColorData._MyColor.RGB
							else nil
					)
				)
			end

			if MyColorData.IsChildrenData then
                -- Object has Parts inside of it, call this function with Object as ParentInstance.
                ColorParts(Part, MyColorData)
			end
		end
	end

	for name, structureData in data do -- Notice this uses the new data structure
		-- find the furniture model with the same name
		local model = furniture:FindFirstChild(name)
		if model then
			-- convert the string CFrame to a real CFrame
			local cf = structureData.objectSpaceCF -- Notice this uses the new data structure
			local components = {}
			for num in string.gmatch(cf, '[^%s,]+') do
				components[#components + 1] = tonumber(num)
			end


            -- Color parts
			ColorParts(model, structureData.partColors)

			-- place the object
			self:Place(model, canvasCF * CFrame.new(unpack(components)), false)
		end
	end

	return self
end

Do note, this is a not very efficient way to do it, it can get expensive with a lot of parts; however it will do what you want it to. Try to only use it as an example/guide.

2 Likes

Forgot to mark this as solved, I found a module that somebody made in #resources category and used it to convert the instance into a table. It’s very simple and useful.

1 Like

What’s the name of the module you’ve used to solve your problem?

Here it is: Converter Module: Instance ↔ Table | Save Instances to DataStores and More!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.