Hello, I’m trying to create a part drag system using tags and I’ve stumbled up on someone’s old part drag model and he is using BodyGyro and BodyPosition to have smooth dragging movement.
This is what I’m trying to accomplish:
(this is from B Ricey’s video)
I have found out that you require attachments for the align stuff, so I have created 2 parts that hold the attachments. I have no idea if this is the right way to do it.
Note: I am using Trove maid module thingy that basically stores instances or connections so I can remove them easier.
My 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)
Also most of those instances are there because I’m trying to make the cool line that connects the part and your cursor, shown in the video.
Thanks in advance!