#drag system (like dead rails!) help!

1 messages · Page 1 of 1 (latest)

marsh field
#

hello! i'm making a drag system using a tutorial by gnomecode, but i've been having trouble having it work. i followed every step, and i've tried my best to understand how it works :(. the script is quite long but i understand it's done by having an attachment 5 Studs away from where the player is looking, and having a part + attachment with a "Draggable" tag be able to be controlled with Alignorientation and AlignPosition. There's also a handler to give ownership of the part to the player and other stuff like having only one person controlling it at a time. if anyone has any insight, please do let me know..

#

local function HighlightObject(object, enableBillboard)
script.Highlight.Adornee = object
local ui = script.BillboardGui

if object then
    ui.txtName.Text = object.Name
end

ui.Enabled = enableBillboard
ui.Adornee = object

end

local function DropObject()
if not grabbedObject then return end
print("dropped")
dragRemote:InvokeServer(target, false)
grabbedObject = nil
script.AlignOrientation.Attachment0 = nil
script.AlignPosition.Attachment0 = nil
end

UserInputService.InputBegan:Connect(function(input, processed)
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end

if target and not grabbedObject then
    --ask server
    if dragRemote:InvokeServer(target, true) then
        print("grabbed")
        grabbedObject = target
    end
else
    DropObject()
end

end)

#

UserInputService.InputEnded:Connect(function(input, processed)
if processed then return end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end

DropObject()

end)

#

RunService.Heartbeat:Connect(function()
if grabbedObject then
local dragAttachment = grabbedObject:FindFirstChild("DragAttachment")
if not dragAttachment then
dragAttachment = Instance.new("Attachment")
dragAttachment.Name = "DragAttachment"
dragAttachment.Parent = grabbedObject
end

    script.AlignPosition.Attachment0 = dragAttachment
    script.AlignOrientation.Attachment0 = dragAttachment
    
    local targetCF = workspace.CurrentCamera.CFrame * CFrame.new(0,0, -5)
    dragTargetAttachment.WorldCFrame = targetCF
    
    HighlightObject(grabbedObject, false)
else
    local mousePosition = UserInputService:GetMouseLocation()
    local ray = workspace.CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterType = Enum.RaycastFilterType.Exclude
    raycastParams.FilterDescendantsInstances = player.Character:GetDescendants()
    local result = workspace:Raycast(ray.Origin, ray.Direction * MAX_DISTANCE, raycastParams)

    if result and result.Instance and result.Instance:HasTag("Draggable") then
        target = result.Instance
        HighlightObject(target, true)
    else
        HighlightObject(nil)
        target = nil
    end
end

end)

#

Handler Script:

local remote = game.ReplicatedStorage.RemoteEvents.DragSystem:WaitForChild("DragRequest")

remote.OnServerInvoke = function(player, object, requestingPickup)
if not object or not object.Parent then return false end

-- Attempting to pick up
if requestingPickup and not object:GetAttribute("DraggedBy") then
    object:SetAttribute("DraggedBy", player.UserId)
    object.Anchored = false
    object:SetNetworkOwner(player)
    return true

    -- Requesting to drop
elseif not requestingPickup and object:GetAttribute("DraggedBy") == player.UserId then
    object:SetAttribute("DraggedBy", nil)
    object:SetNetworkOwner(nil)
    -- Optional: reset velocity
    object.AssemblyLinearVelocity = Vector3.zero
    object.AssemblyAngularVelocity = Vector3.zero
    return true
end

return false

end

#

i can send over the studio file if anyone wants to take a look. sorry if this is a handful

young flicker
#

is it not anchored

tender mantle
#

Clickdetector

marsh field
#

It's unanchored

marsh field
lethal jay
#

dragdetector

marsh field
sick patrolBOT
#

studio** You are now Level 2! **studio

marsh field
#

the closest i've gotten with that method is with the wiewplane option but then it'd be up on the face

fallow slate
#

do i see an error there

marsh field
#

referencing is correct, invokeserver and parameters seem right