Moving multiple parts at the same time using CFrame

Hi there, I’m trying to move multiple parts at once by welding them to an “Origin” part and moving that part. In my mind, the parts should all be moving with the origin, but it’s not quite working out. The origin moves fine but the other parts don’t budge :confused:… any ideas?

Thanks!

My code: (I’m using functions inside a ModuleScript and calling them in another script)

--Weld the components together
module.weldComponents = function(group)
	local origin = group:WaitForChild("Origin")
	
	for _, part in pairs(group:GetDescendants()) do
		if part:IsA("BasePart") then
			print(part.Name)
			local weld = Instance.new("WeldConstraint", part)
			weld.Part0 = part
			weld.Part1 = origin
		end
	end
end

--Move the components to the right place
module.moveComponents = function(target, group)
	local targetPos = CFrame.new(target.Position)
	group.Origin.CFrame = targetPos
end
1 Like

Try using group:SetPrimaryPartCFrame(cframe here) to move all of the parts inside that group

5 Likes

Thanks, it works great! It’s nice that it also removes the need for welds.

Final code: (I’m getting the parts from ServerStorage, adding them to Workspace, then positioning them)

--Add the components to the dummy
module.addComponents = function(unclaimed, components)
	for _, dummy in pairs(unclaimed:GetChildren()) do
		if dummy.Name == "Dummy" then
			
			local group = Instance.new("Model")
			group.Name = "Components"
			group.Parent = dummy
			
			for j, component in pairs(components:GetChildren()) do
				local dupe = component:Clone()
				dupe.Parent = dummy.Components
			end
			
			dummy.Components.PrimaryPart = dummy.Components.Origin
		end
	end
end

--Move the components to the right place
module.moveComponents = function(target, group)
	local targetPos = CFrame.new(target.Position)
	group:SetPrimaryPartCFrame(targetPos)
end