ClassName, Color, Material, Transparency, Size, Position, and Orientation all repeat for parts, unions, and vehicleseats. I also have extra things that I seperated such as Shape and UsePartColor. However, they don’t repeat for the following things, so I can’t up them at the top. Thanks!
Could it be possible to create a function that returns a table containing those instances instead?
i.e.:
-- TYPE DEFINITION
-- This is assumed based on the function returns.
type SerializedPartData = {
ClassName : string,
Color : string,
MaterialName : string, -- Assumed, no public docs on this
Transparency : number,
Size : string,
Position : string,
Orientation : string
}
-- FUNCTION DEFINITION
-- [CreateSerializedBasePartData] - Return a table of serialized data representing a BasePart or any subclass thereof.
--[[
Return a table of serialized data representing a BasePart or any subclass thereof. <br>
<strong>part</strong> : The part to create a serialized data table from.
]]
function CreateSerializedBasePartData(part : BasePart) : SerializedPartData
local partData : SerializedPartData = {}
-- Many ways to do this, this is just one
partData.ClassName = part.ClassName
partData.Color = part.Color:ToHex()
partData.Material = part.Material.Name -- accidentally missed this, pointed out by Guest_2000123145.
partData.Transparency = part.Transparency
partData.Size = tostring(part.Size)
partData.Position = tostring(part.Position)
partData.Orientation = tostring(part.Orientation)
return partData
end
-- ...Where Your Code is...
if (item.ClassName == "Part") then
serializedDataParts[instanceName].Properties = CreateSerializedBasePartData(part)
serializedDataParts[instanceName].Properties["Shape"] = item.Shape.Name
elseif (item.ClassName == "UnionOperation") then
serializedDataParts[instanceName].Properties = CreateSerializedBasePartData(part)
serializedDataParts[instanceName].Properties["Shape"] = item.Shape.Name
elseif (item.ClassName == "VehicleSeat") then
serializedDataParts[instanceName].Properties = CreateSerializedBasePartData(part)
-- etc
-- Note: As I don't know if you use type checking, I'm not sure if this will work entirely, but it is how I would approach this problem. Hopefully the function and type definition at least help.
the : just makes it so roblox can autofill the children of the variable. for example, when typing partData.Co it will allow you to autocomplete to color. it is not necassary but definitely a good habit to have.