I’m working on a realistic dragging system where a player can pick up and drag a part. Currently, if the part gets too far from the player, I use Lerp
to pull it back. However, this approach causes buggy behavior—sometimes the part gets pushed even further away instead of smoothly following the player.
My Goal
I want the part to follow the player naturally when dragged, and if it gets too far, it should be pulled back smoothly without jittering or being pushed away. If I could also make the dragging more smooth/realistic that would be great also.
any help/ideas are very appreciated.
Script
local part = script.Parent
local BreakPoint = part:WaitForChild("BreakPoint")
local BreakSound = part:WaitForChild("Break")
local speedThreshold = 12.5
local DragDetector = part:WaitForChild("DragDetector")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VFXFolder = ReplicatedStorage:WaitForChild("VFX")
local SmallBreakVfx = VFXFolder:WaitForChild("SmallBreak")
local DragLogic = ReplicatedStorage:WaitForChild("DragDetector")
local Debris = game:GetService("Debris")
local debounce = false
local stopRange = 15
local dragRange = 8
local function onTouched(otherPart)
local currentSpeed = part.Velocity.Magnitude
if currentSpeed > speedThreshold and not debounce then
debounce = true
print("Break effect")
BreakSound:Play()
for _, vfx in ipairs(SmallBreakVfx:GetChildren()) do
if vfx:IsA("ParticleEmitter") then
local clone = vfx:Clone()
clone.Parent = BreakPoint
clone:Emit(2)
Debris:AddItem(clone, 2)
end
end
delay(1, function()
debounce = false
end)
end
end
part.Touched:Connect(onTouched)
DragDetector.DragStart:Connect(function(player)
print("Dragging Started")
end)
DragDetector.DragEnd:Connect(function(player)
print("Dragging Stopped")
end)
DragDetector.DragContinue:Connect(function(player)
local character = player.Character
if not character then return end
local hrp = character:FindFirstChild("HumanoidRootPart")
if not hrp then return end
local distance = (part.Position - hrp.Position).Magnitude
if distance > stopRange then
print("Drag canceled: part is too far from the player")
DragDetector.Enabled = false
task.wait(0.5)
DragDetector.Enabled = true
return
end
if distance > dragRange then
local direction = (part.Position - hrp.Position).Unit
local targetPos = hrp.Position + direction * dragRange
part.Position = part.Position:Lerp(targetPos, 5)
print("Part too far: Dragging")
else
print("No adjustment needed")
end
end)
Video Showcase