I need help with drag parts script

I’m trying to make a drag part script similarly to lumber tycoon 2 one.

or the one from B Ricey’s video:
image

The problem is that roblox has deprecated the bodygyro and bodyposition and now recommends people to use PositionAlign and OrientationAlign which are almost completely different. And those two deprecated things were used to make lumber tycoon 2 drag part script.

I have no idea how to make the same thing with the new Align objects.

Thanks in advance.

My failed script:


local UIP = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local TS = game:GetService("TweenService")

local Trove = require(game.ReplicatedStorage.Modules.Trove).new()

local dragging = false
local Target:Part
local distanceFromCamera = 1
local reachDistance = 10

local plr = game.Players.LocalPlayer

plr.CharacterAdded:Wait()

local Character = plr.Character
local Torso:MeshPart = Character:WaitForChild("Torso")
local Head:MeshPart = Character:WaitForChild("Head")
local Mouse = plr:GetMouse()

UIP.InputBegan:Connect(function(Input,inText)
	if inText then return end
	if Input.UserInputType == Enum.UserInputType.MouseButton1 and not dragging then
		if table.find(CollectionService:GetTags(Mouse.Target),"Draggable") then
			if (Mouse.Target.Position-Torso.Position).Magnitude <= reachDistance then
				dragging = true
				local Target = Mouse.Target
				Target.Anchored = false
				Mouse.TargetFilter = Target
				local Grab:Part = Trove:Add(Instance.new("Part"))
				Grab.Parent = workspace
				Grab.Shape = Enum.PartType.Ball
				Grab.CanCollide = false
				Grab.CFrame = Mouse.Hit
				Grab.BottomSurface = Enum.SurfaceType.Smooth
				Grab.TopSurface = Enum.SurfaceType.Smooth
				Grab.Size = Vector3.new(.5,.5,.5)
				Grab.Material = Enum.Material.Neon
				Grab.BrickColor = BrickColor.new("Lime green")
				Grab.Name = "Grab"
				local TempPart:Part = Trove:Add(Instance.new("Part"))
				TempPart.Parent = workspace
				TempPart.Transparency = 1
				TempPart.CanCollide = false
				TempPart.CFrame = Mouse.Hit
				TempPart.Name = "TempPart"
				local Gyro:Part = Trove:Add(Instance.new("Part"))
				Gyro.Parent = workspace
				Gyro.Size = Grab.Size
				Gyro.CanCollide = false
				Gyro.Anchored = true
				Gyro.Transparency = 1
				Gyro.Name = "Gyro"
				local BPos:Part = Trove:Add(Instance.new("Part"))
				BPos.Parent = workspace
				BPos.Size = Grab.Size
				BPos.CanCollide = false
				BPos.Anchored = true
				BPos.Transparency = 1
				BPos.Name = "BPos"
				local A0:Attachment = Trove:Add(Instance.new("Attachment"))
				A0.Parent = Target
				local A1:Attachment = Trove:Add(Instance.new("Attachment"))
				A1.Parent = Gyro
				local A2:Attachment = Trove:Add(Instance.new("Attachment"))
				A2.Parent = Grab
				local A3:Attachment = Trove:Add(Instance.new("Attachment"))
				A3.Parent = TempPart
				local Weld:Weld = Trove:Add(Instance.new("Weld"))
				Weld.Parent = Grab
				Weld.Part0 = Grab
				Weld.Part1 = Target
				local Sparkles = Trove:Clone(game.ReplicatedStorage.Effects.Sparkles)
				Sparkles.Parent = Grab
				local Rod:RodConstraint = Trove:Add(Instance.new("RodConstraint"))
				Rod.Parent = Grab
				Rod.Color = Grab.BrickColor
				Rod.Thickness = 1
				Rod.Attachment0 = A2
				Rod.Attachment1 = A3
				Rod.Name = "Rod"
				local PosAlign:AlignPosition = Trove:Add(Instance.new("AlignPosition"))
				PosAlign.Parent = Target
				PosAlign.Attachment0 = A0
				PosAlign.Attachment1 = A1
				local OrientAlight:AlignOrientation = Trove:Add(Instance.new("AlignOrientation"))
				OrientAlight.Parent = Target
				OrientAlight.Attachment0 = A0
				OrientAlight.Attachment1 = A1
				Trove:Connect(RunService.Heartbeat,function()
					local X,Y,Z = Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z
					local CF1 = CFrame.new(Head.CFrame.Position,Vector3.new(X,Y,Z))
					local CF2 = CF1 * CFrame.new(0, 0, -4)
					BPos.Position = Vector3.new(CF2.X, CF2.Y, CF2.Z)
					Gyro.CFrame = game.Workspace.CurrentCamera.CFrame
				end)
			end
		end
	end
end)

UIP.InputEnded:Connect(function(Input,inText)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if inText then return end
		dragging = false
		Trove:Clean()
		Mouse.TargetFilter = nil
	end
end)

I have fixed it:

local UIP = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local TS = game:GetService("TweenService")

local Trove = require(game.ReplicatedStorage.Modules.Trove).new()

local dragging = false
local midRotation = false
local movement = false

local defaultDistancefromPlr = 10
local distanceFromPlr = defaultDistancefromPlr
local reachDistance = 15
local rotationDegrees = 45
local moveIncrement = 1
local ThrowForce = 8

local plr = game.Players.LocalPlayer
plr.CharacterAdded:Wait()
local Character = plr.Character
local Torso:MeshPart = Character:WaitForChild("Torso")
local Head:MeshPart = Character:WaitForChild("Head")
local Mouse = plr:GetMouse()
local Camera = workspace.CurrentCamera

local Target:Part
local Aori:Attachment
local A0:Attachment

local RotX = 0
local RotY = 0

local function Cleanup()
	if dragging then
		Trove:Clean()
		Mouse.TargetFilter = nil
		if Target then
			Target.CollisionGroup = "Default"
		end
		Target = nil
		Aori = nil
		RotY = 0
		RotX = 0
		distanceFromPlr = defaultDistancefromPlr
		dragging = false
	end
end

UIP.InputBegan:Connect(function(Input,inText)
	if inText then return end
	if Input.UserInputType == Enum.UserInputType.MouseButton1 and not dragging then
		if table.find(CollectionService:GetTags(Mouse.Target),"Draggable") then
			if (Mouse.Target.Position-Torso.Position).Magnitude <= reachDistance then
				dragging = true
				Target = Mouse.Target
				Target.Anchored = false
				Target.CollisionGroup = "Grabbed"
				local x,y,z = Target.Orientation.X,Target.Orientation.Y,Target.Orientation.Z
				A0 = Trove:Add(Instance.new("Attachment",Target))
				A0.WorldPosition = Mouse.Hit.Position
				local PBall:Part = Trove:Add(Instance.new("Part",Target))
				PBall.TopSurface = Enum.SurfaceType.Smooth
				PBall.BottomSurface = Enum.SurfaceType.Smooth
				PBall.Size = Vector3.new(.25,.25,.25)
				PBall.CanCollide = false
				PBall.Color = Color3.new(1, 1, 1)
				PBall.Material = Enum.Material.Neon
				PBall.Shape = Enum.PartType.Ball
				PBall.Name = "Epicenter"
				PBall.Transparency = .5
				Mouse.TargetFilter = Target
				local Apos:Attachment = Trove:Add(Instance.new("Attachment",workspace.Terrain))
				Aori = Trove:Add(Instance.new("Attachment",workspace.Terrain))
				local AlignP:AlignPosition = Trove:Add(Instance.new("AlignPosition",Target))
				AlignP.Attachment0 = A0
				AlignP.Attachment1 = Apos
				AlignP.Responsiveness = 25
				local AlignO:AlignOrientation = Trove:Add(Instance.new("AlignOrientation",Target))
				AlignO.Attachment0 = A0
				AlignO.Attachment1 = Aori
				AlignO.Responsiveness = 25
				local MouseBall:Part = Trove:Add(Instance.new("Part",Target))
				MouseBall.TopSurface = Enum.SurfaceType.Smooth
				MouseBall.BottomSurface = Enum.SurfaceType.Smooth
				MouseBall.Size = Vector3.new(.25,.25,.25)
				MouseBall.CanCollide = false
				MouseBall.Color = Color3.new(1, 1, 1)
				MouseBall.Material = Enum.Material.Neon
				MouseBall.Shape = Enum.PartType.Ball
				MouseBall.Name = "Cursor"
				MouseBall.Transparency = .5
				local AMouse:Attachment = Trove:Add(Instance.new("Attachment",workspace.Terrain))
				local Rope:Part = Trove:Add(Instance.new("Part",Target))
				Rope.TopSurface = Enum.SurfaceType.Smooth
				Rope.BottomSurface = Enum.SurfaceType.Smooth
				Rope.Color = Color3.new(1, 1, 1)
				Rope.CanCollide = false
				Rope.Material = Enum.Material.Neon
				Rope.Name = "Connection"
				Rope.Transparency = .5
				Trove:Connect(RunService.Heartbeat,function()
					local X,Y,Z = Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z
					local CF1 = CFrame.new(Head.CFrame.Position,Vector3.new(X,Y,Z))
					local CF2 = CF1 * CFrame.new(0, 0, -distanceFromPlr)
					Apos.CFrame = CFrame.new(CF2.X, CF2.Y, CF2.Z)
					--local CRX = game.Workspace.CurrentCamera.CFrame.Rotation.X
					--local CRZ = game.Workspace.CurrentCamera.CFrame.Rotation.Z
					Aori.Orientation = Vector3.new(x+RotX,y+RotY,z)
					AMouse.CFrame = CFrame.new(CF2.X, CF2.Y, CF2.Z)
					MouseBall.CFrame = AMouse.CFrame
					local RopeSizeZ = (A0.WorldPosition-MouseBall.Position).Magnitude
					local RopePos = CFrame.new(A0.WorldPosition,MouseBall.Position)
					RopePos *= CFrame.new(0,0,-RopeSizeZ*.5)
					Rope.CFrame = RopePos
					Rope.Size = Vector3.new(.1,.1,RopeSizeZ)
					PBall.Position = A0.WorldPosition
				end)
			end
		end
	else
		if not Target or midRotation or not Aori then return end
		if Input.KeyCode == Enum.KeyCode.R and dragging then
			midRotation = true
			local Rot = Aori.CFrame.Rotation
			Aori.CFrame = CFrame.Angles(Rot.X,Rot.Y+rotationDegrees,Rot.Z)
			RotY += rotationDegrees
			midRotation = false
		elseif Input.KeyCode == Enum.KeyCode.T and dragging then
			midRotation = true
			local Rot = Aori.CFrame.Rotation
			Aori.CFrame = CFrame.Angles(Rot.X+rotationDegrees,Rot.Y,Rot.Z)
			RotX += rotationDegrees
			midRotation = false
		elseif Input.KeyCode == Enum.KeyCode.F and dragging then
			local dir = Mouse.Hit.LookVector
			local power = ThrowForce*Target:GetMass()
			Target.AssemblyLinearVelocity = dir*power
			Cleanup()
		elseif Input.KeyCode == Enum.KeyCode.Q and dragging then
			movement = true
			while movement do
				task.wait(.05)
				distanceFromPlr = math.clamp(distanceFromPlr-moveIncrement,5,reachDistance)
			end
		elseif Input.KeyCode == Enum.KeyCode.E and dragging then
			movement = true
			while movement do
				task.wait(.05)
				distanceFromPlr = math.clamp(distanceFromPlr+moveIncrement,5,reachDistance)
			end
		end
	end
end)

UIP.InputEnded:Connect(function(Input,inText)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if inText then return end
		Cleanup()
	elseif Input.KeyCode == Enum.KeyCode.Q or Input.KeyCode == Enum.KeyCode.E then
		movement = false
	end
end)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.