function buildRigFromAttachments(S,z)for m,r in pairs(z:GetChildren())do if r:IsA("\065\116\116\097\099\104\109\101\110\116")and string.find(r.Name,"\082\105\103\065\116\116\097\099\104\109\101\110\116")then for m,Y in pairs(z.Parent:GetChildren())do if Y~=z and Y~=S then local S=Y:FindFirstChild(r.Name)if S then buildJoint(r,S)buildRigFromAttachments(z,S.Parent)end end end end end end local function S(S,z)for S,m in pairs(S:GetChildren())do if m:IsA("\067\104\097\114\097\099\116\101\114\077\101\115\104")and m.BodyPart==z.BodyPart then return m end end return nil end
-- Removes old Motor6Ds and builds the rig from the attachments in the parts
-- Call this with nil, HumanoidRootPart
function buildRigFromAttachments(last, part)
	for _, attachment in pairs(part:GetChildren()) do
		if attachment:IsA("Attachment") and string.find(attachment.Name, "RigAttachment") then
			for _, sibling in pairs(part.Parent:GetChildren()) do
				if sibling ~= part and sibling ~= last then
					local matchingAttachment = sibling:FindFirstChild(attachment.Name)
					if matchingAttachment then
						buildJoint(attachment, matchingAttachment)
						-- Continue the recursive tree traversal building joints
						buildRigFromAttachments(part, matchingAttachment.Parent)
					end
				end
			end
		end
	end
end

local function getOldCharacterMesh(character, newCharacterMesh)
	for _, obj in pairs(character:GetChildren()) do
		if obj:IsA("CharacterMesh") and obj.BodyPart == newCharacterMesh.BodyPart then
			return obj
		end
	end
	return nil
end
