Does anyone know how to condense this?


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!

1 Like

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.
1 Like

One second, ROBLOX doesn’t seem to like the editing of code blocks right now.

Edit: Done. Turns out Tab no longer works in Devforum for my browser. Weird.

Before I try to put this in my game, can you explain what
local partData : SerializedPartData = {} does? Thanks!

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.

Yeah, and it’s also super helpful when you’re documenting things too, so that others can understand your code!

you forgot “material” in your CreateSerializedBasePartData function :joy:
apart from that, it works :D

anywhere I can read up on this : usage? Thanks!

I’ll edit it with a comment acknowledging this!

Also: Type Checking | Documentation - Roblox Creator Hub

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