I am trying to create a part that copies another parts properties in my table from size to position etc. But it skips only one which is Anchored property I tried printing it does not get past my if statements. Please help me as much as you can.
function functionMod:Convert(instancePart,class)
local properties = {
"Size";
"Position";
"Orientation";
"Anchored";
"BrickColor";
"Transparency";
"Reflectance";
"Material";
"CanCollide";
}
local newClass = Instance.new(class)
newClass.Parent = instancePart.Parent
newClass.Name = instancePart.Name
for _, prop in pairs(properties) do
if instancePart[prop] and newClass[prop] then
newClass[prop] = instancePart[prop]
end
end
instancePart:Destroy()
end
The reason for this is because of the way you are checking if the properties exist. Specifically this line:
if instancePart[prop] and newClass[prop] then
Basically the anchored property has a value of either true or false. When you get to the part of your loop where you check the anchored property you are checking if it is true on both the instancePart and newClass which isn’t what you intended to do. The correct way to check if a property exists is using pcall.
local success = pcall(function()
_ = instancePart[prop]
_ = newClass[prop]
end)
if success then
newClass[prop] = instancePart[prop]
end
What this code does is attempt to set the variable _ to the value of the currently looped property of instancePart and of newClass. If it fails when doing this with either of them then success is false. If it successfully sets the variable for both then success is true and we know the properties exist.