Module Script Help On Function

I want to make my module script copy a parts properties

It completely skips the Anchored property in my part

I went on youtube.com and devforum.roblox.com

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

I hope you can help me :grinning_face_with_smiling_eyes:

Is there any errors in the output if so please screenshot.

No and ok:

@Axyanz :grinning_face_with_smiling_eyes:

No error as you can see :grinning_face_with_smiling_eyes:

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.

1 Like

Where would I add it sorry? :grinning_face_with_smiling_eyes:

You would replace your current code within the loop for checking if the properties exist

1 Like