How can I move a welded model?

  1. What do you want to achieve?
    I am trying to make each turret point at Mouse.Target.

  2. What is the issue?
    When I set the CFrame of the turret’s primary part to face Mouse.Target, the entire model moves instead of each individual turret.
    https://gyazo.com/de578ed8ce9f5bd3b612fbc2d267759a

  3. What solutions have you tried so far?
    I am using WeldConstraints and thought if I welded all the turret’s part to a RootPart, then weld that root part to a turret base, and weld the turret base to the ship RootPart that when I change the CFrame of the turret to face Mouse.Target, the turret would look at the target. Unfortunately, this did not work.

Here is the code I am using:

-- In a server script.
function WeldPair(x, y)
	local weld = Instance.new("WeldConstraint") 
	weld.Part0 = x
	weld.Part1 = y
	weld.Parent = x
end

local function weldModel(model, plr)
	for _,v in pairs(model:GetDescendants()) do
		if v:IsA("BasePart") and not v:FindFirstChildWhichIsA("WeldConstraint") then
			v.Anchored = false
			WeldPair(model.PrimaryPart, v)
			v:SetNetworkOwner(plr)
		end
	end
end

local function generateShip(player)
	for _, turretRoot in pairs(ship.TurretSlots:GetChildren()) do
		if turretRoot:IsA("Model") then
			local turret = workspace.CompletedTurrets.Turret1:Clone()
			turret.Name = "main"
			turret.Parent = turretRoot
			turret:SetPrimaryPartCFrame(turretRoot.PrimaryPart.CFrame)
			
			WeldPair(turret.PrimaryPart, turret.Middle)
			WeldPair(turret.Middle, turretRoot.PrimaryPart)
			WeldPair(turretRoot.PrimaryPart, ship.PrimaryPart)
			weldModel(turret, player)
		end
	end
	
	weldModel(ship, player)
end
-- In a local script.
function lookAt(target, eye)
	local forwardVector = (eye - target).Unit
	local upVector = Vector3.new(0, 1, 0)
	local rightVector = forwardVector:Cross(upVector)
	local upVector2 = rightVector:Cross(forwardVector)

	return CFrame.fromMatrix(eye, rightVector, upVector2)
end

function turretFire()
	if clientShip then
		mouse.Button1Down:Connect(function()
			local target = nil
			target = mouse.Target

			if (target) and target:IsDescendantOf(workspace.Asteroid) then
				for _, turret in pairs(clientShip.TurretSlots:GetChildren()) do
					local mainTurret = turret.main
					
					mainTurret.PrimaryPart.CFrame = lookAt(target.Position, mainTurret.PrimaryPart.Position)
				end
			end
		end)
	end
end

Any help is appreciated, thanks!

You could use a Weld instead of a WeldConstraint and change C0 or C1 instead of the part’s CFrames, or use a Motor6D and change Transform, or use a different constraint or combination of constraints (like a HingeConstraint | Documentation - Roblox Creator Hub with an actuator type of servo).

Also BTW

Just doing CFrame.lookAt(eye, target) does the same thing :slight_smile:

1 Like

Just like @nicemike40 said you will want to use a weld or better yet a Motor6D which is meant to move. However eventually you will want to add constraints to the turrets in order to make them not look at targets at an impossible angle so here is my resource which does the C0 and C1 stuff automatically for you and all you need to adjust it is to setup the attachments.

1 Like

Thank you for this module, it helped a lot!