Why is this function i made not working?

local function DecodePart(PartTable)
	
	if PartTable[#PartTable] == "BasePart" then
		local iInstance = Instance.new("Part")
		iInstance.CastShadow = PartTable["CastShadow"]
		iInstance.Color = PartTable["Color"]
		iInstance.Material = PartTable["Material"]
		iInstance.Reflectance = PartTable["Reflectance"]
		iInstance.Transparency = PartTable["Transparency"]
		iInstance.Size = PartTable["Size"]
		iInstance.CFrame = PartTable["CFrame"]
		iInstance.Shape = PartTable["Shape"]
		return iInstance
	elseif PartTable[#PartTable] == "UnionOperation" then
		local iInstance = Instance.new("UnionOperation")
		iInstance.CastShadow = PartTable["CastShadow"]
		iInstance.Color = PartTable["Color"]
		iInstance.Material = PartTable["Material"]
		iInstance.Reflectance = PartTable["Reflectance"]
		iInstance.Transparency = PartTable["Transparency"]
		iInstance.SmoothingAngle = PartTable["SmoothingAngle"]
		iInstance.UsePartColor = PartTable["UsePartColor"]
		iInstance.Size = PartTable["Size"]
		iInstance.CFrame = PartTable["CFrame"]
		return iInstance
	elseif PartTable[#PartTable] == "MeshPart" then
		local iInstance = Instance.new("MeshPart")
		iInstance.CastShadow = PartTable["CastShadow"]
		iInstance.Color = PartTable["Color"]
		iInstance.DoubleSided = PartTable["DoubleSided"]
		iInstance.Material = PartTable["Material"]
		iInstance.MeshId = PartTable["MeshId"]
		iInstance.Reflectance = PartTable["Reflectance"]
		iInstance.TextureID = PartTable["TextureID"]
		iInstance.Transparency = PartTable["Transparency"]
		iInstance.Size = PartTable["Size"]
		iInstance.CFrame = PartTable["CFrame"]
		return iInstance
	elseif PartTable[#PartTable] == "Decal" then
		local iInstance = Instance.new("Decal")
		iInstance.Color3 = PartTable["Color3"]
		iInstance.Texture = PartTable["Texture"]
		iInstance.Transparency = PartTable["Transparency"]
		iInstance.ZIndex = PartTable["ZIndex"]
		iInstance.Face = PartTable["Face"]
		return iInstance
	elseif PartTable[#PartTable] == "Texture" then
		local iInstance = Instance.new("Texture")
		iInstance.Color3 = PartTable["Color3"]
		iInstance.OffsetStudsU = PartTable["OffsetStudsU"]
		iInstance.OffsetStudsV = PartTable["OffsetStudsV"]
		iInstance.StudsPerTileU = PartTable["StudsPerTileU"]
		iInstance.StudsPerTileV = PartTable["StudsPerTileV"]
		iInstance.Texture = PartTable["Texture"]
		iInstance.Transparency = PartTable["Transparency"]
		iInstance.ZIndex = PartTable["ZIndex"]
		iInstance.Face = PartTable["Face"]
		return iInstance
	else
		return "What ?"
	end
	
	

end

this function decodes a table and turns it into a part, although it tried to return nil (i added a back, that is the “What?”)

This is what an input table looks like for it:

But it cant recognize the type?

Is the table output screenshot the table you use for PartTable, or something else? If it’s the same, you’re trying to get the length of a dictionary with PartTable[#PartTable] instead of PartTable.Type.

im getting the lencgth because type is always the last index, and that table output is the one going into the func

This is not how dictionary keys work in Luau. Getting the length of a dictionary via the # is not possible, only via proper arrays.

Why not just pass the type to the function along with the table?

because i have to have all the data to recreate the part

oh wait nvm i see what ur saying lemme try that

just tried it, its still not working

You’re hardcoding this a LOT. Honestly, I recommend rewriting it entirely more optimized and cleaned up. But the main issue is you’re using #'s for tables and you can’t.

And all of that information would still exist within the function, all you need do is pass the type as a second input to the function:

local function DecodePart(PartTable,PartType)

	if PartType == "BasePart" then
	...
end

yeah i just did that, its printing out as nil?

oh my god i was passing the wrong table…
i was printing the right table but sending the wrong one, ill respond it it works now

yes but im having issues with meshes, mesh id and the double sided property:
image
(Not material thats fine)
it says i need a plugin to do it?

Not sure, are you sure that DoubleSided and MeshId are being assigned from a correct storage container?

MeshIds and not directly changeable. You’ll have to insert a copy of the mesh via InsertService:CreateMeshPartAsync() instead. The property DoubleSided is still unchangeable with this, however.