Im making a drag system but when I started testing on players, it would just let go and glitching
What It should look normally
The bug
I’ve tried mouse filter but it doesn’t work since it only does for 1 model, tried looking for similar problems in yt/ devfourm, still nope.
Code:
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local GrabObject = nil
local GrabStart = false
local Dragger = nil
local RotatingR = false
local RotatingT= false
local Player = Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera
wait()
function weldBetween(a, b)
local weld = Instance.new("ManualGlue", a)
weld.C0 = a.CFrame:inverse() * b.CFrame
weld.Part0 = a
weld.Part1 = b
return weld
end
function AddMover(part)
local NewMover = Instance.new("BodyPosition")
NewMover.Parent = part
NewMover.MaxForce = Vector3.new(40000,40000,40000)
NewMover.P = 40000
NewMover.D = 1000
NewMover.Position = part.Position
NewMover.Name = "Mover"
local NewRot = Instance.new("BodyGyro")
NewRot.Parent = part
NewRot.MaxTorque = Vector3.new(3000,3000,3000)
NewRot.P = 3000
NewRot.D = 500
NewRot.CFrame = game.Workspace.CurrentCamera.CFrame
NewRot.Name = "RotMover"
local RotOffset = Instance.new("CFrameValue")
RotOffset.Name = "RotOffset"
RotOffset.Parent = part
end
function CreateDragBall()
local DragBall = Instance.new("Part")
DragBall.Size = Vector3.new(0.1, 0.1, 0.1)
DragBall.Shape = "Ball"
DragBall.Name = "DragBall"
DragBall.Parent = Player.Character
DragBall.Transparency = 1
return DragBall
end
function Grab(ActionName, UserInputState, InputObject)
if ActionName == "Grab" then
if UserInputState == Enum.UserInputState.Begin then
local Magnitude = (Mouse.Hit.Position - Character.Head.CFrame.Position).magnitude
if Magnitude < 15 then
if Mouse.Target then
GrabObject = Mouse.Target
if GrabObject.Anchored == false then
GrabStart = true
local DragBall = CreateDragBall()
DragBall.CFrame = Mouse.Hit
Dragger = DragBall
Mouse.TargetFilter = GrabObject
local DragBallWeld = weldBetween(DragBall,GrabObject)
AddMover(DragBall)
while Dragger do
local CF = CFrame.new(Character.Head.Position, Mouse.Hit.Position)
Dragger.Mover.Position = (CF + (CF.LookVector * 10)).Position
Dragger.RotMover.CFrame = Camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
wait()
end
Mouse.TargetFilter = nil
end
end
end
elseif UserInputState == Enum.UserInputState.End then
GrabObject = nil
GrabStart = false
if Dragger then
Dragger:Destroy()
Dragger = nil
end
end
elseif ActionName == "RotateF" then
if GrabStart == true then
if Dragger then
if UserInputState == Enum.UserInputState.End then
local Info = TweenInfo.new(0.5, Enum.EasingStyle.Circular, Enum.EasingDirection.In)
local Goal = {Orientation = Vector3.new(0, 0, 0)}
TweenService:Create(GrabObject, Info, Goal):Play()
end
end
end
elseif ActionName == "RotateR" then
if GrabStart == true then
if UserInputState == Enum.UserInputState.Begin then
RotatingR = true
while RotatingR and Dragger do
GrabObject.Orientation = GrabObject.Orientation + Vector3.new(0, 1, 0)
wait()
end
elseif UserInputState == Enum.UserInputState.End then
RotatingR = false
end
end
elseif ActionName == "RotateT" then
if GrabStart == true then
if UserInputState == Enum.UserInputState.Begin then
RotatingT = true
while RotatingT and Dragger do
GrabObject.Orientation = GrabObject.Orientation + Vector3.new(0, 0, 1)
wait()
end
elseif UserInputState == Enum.UserInputState.End then
RotatingT = false
end
end
end
end
ContextActionService:BindAction("Grab", Grab, true, Enum.UserInputType.MouseButton1)
ContextActionService:BindAction("RotateF", Grab, true, Enum.KeyCode.F)
ContextActionService:BindAction("RotateR", Grab, true, Enum.KeyCode.R)
ContextActionService:BindAction("RotateT", Grab, true, Enum.KeyCode.T)