Help with DragDetectors

What’s up devs! right now I’m trying to make so that my DragDetectors have a constrained minimum and maximum distance from the player. I am trying to make the carry systems of parts similar to that of the Roblox game, “3008 [2.72]” (Link for the game: 3008 [2.72] - Roblox) Also, here is the script that I’m currently using. You can test it if you put it in a ServerScript in ServerScriptService with at least one unanchored part in the workspace:

local Players = game.Players

wait(2)

local function stickToPart(part, targetPart)
	local touching = true
	part.Position = targetPart.Position
	part.TouchEnded:Connect(function()
		touching = false
	end)
	repeat
		wait() -- Avoids busy-waiting
	until not touching
end

for i, part in pairs(workspace:GetDescendants()) do
	if part:IsA("BasePart") and not part.Anchored then
		local currentRightArm, currentLeftArm, currentRightShoulder, currentLeftShoulder, currentTorso
		local dragDetector = Instance.new("DragDetector")
		dragDetector.DragStyle = Enum.DragDetectorDragStyle.TranslateViewPlane
		dragDetector.ResponseStyle = Enum.DragDetectorResponseStyle.Physical
		dragDetector.ApplyAtCenterOfMass = true
		dragDetector.MaxTorque = math.huge
		dragDetector.Responsiveness = 50
		dragDetector.Parent = part

		local rightWeldConstraint = Instance.new("WeldConstraint")
		local leftWeldConstraint = Instance.new("WeldConstraint")

		dragDetector.DragStart:Connect(function(player)
			if player then
				local char = player.Character or player.CharacterAdded:Wait()
				local torso = char:WaitForChild("Torso")
				local rightArm = char:WaitForChild("Right Arm")
				local leftArm = char:WaitForChild("Left Arm")

				local rightShoulder = torso:WaitForChild("Right Shoulder")
				local leftShoulder = torso:WaitForChild("Left Shoulder")

				currentRightArm = rightArm
				currentLeftArm = leftArm
				currentRightShoulder = rightShoulder
				currentLeftShoulder = leftShoulder
				currentTorso = torso
				
				torso:WaitForChild("Right Shoulder").Enabled = false
				torso:WaitForChild("Left Shoulder").Enabled = false
				
				rightArm.Position = part.Position + (torso.CFrame.RightVector * (part.Size.Magnitude / 2))
				leftArm.Position = part.Position - (torso.CFrame.RightVector * (part.Size.Magnitude / 2))

				rightArm.CFrame *= CFrame.Angles(math.rad(90), 0, 0)
				leftArm.CFrame *= CFrame.Angles(math.rad(90), 0, 0)
				
				rightWeldConstraint.Part0 = rightArm
				rightWeldConstraint.Part1 = part
				rightWeldConstraint.Parent = rightArm

				leftWeldConstraint.Part0 = leftArm
				leftWeldConstraint.Part1 = part
				leftWeldConstraint.Parent = leftArm
			end
		end)
		
		dragDetector.DragEnd:Connect(function(player)
			print('started')
			rightWeldConstraint.Parent = nil
			leftWeldConstraint.Parent = nil
			print('weld finished')
			
			currentRightArm.Anchored = false
			currentLeftArm.Anchored = false
			print('arms finished')
			
			task.wait(.1)
			currentRightShoulder.Enabled = true
			currentLeftShoulder.Enabled = true
			print('shoulders finished')
		end)
	end
end

Another couple of mechanics I would like to add: make so that the detached arms orientate the draggable object towards the camera (it’s meant to be a first person game). If you think that it would be better to try something else than dragdetectors please let me know. Also, if you have any suggestions for the code please do not hesitate to mention it. Thanks for reading!

1 Like

You can check the magnitude between the part and the character, and then move the part accordingly. You can also try to clamp the positions values in .Heartbeat.

By the way, instead of using a repeat loop here:

You can do:

part.Position = targetPart.Position
part.TouchEnded:Wait()