How Do I Allow a Player to Pull and Move A Cart?

Hey there! I am working on a border game. I want there to be a feature where players can have a cart where they can put stuff in. I want this cart to be draggable, but I do mean with a drag-detector, I want it to sort of walk with the player. There is a ProximityPrompt where the player can grab it. I don’t really know where to start for this and I was wondering if anyone could help me.

image

image

I’ve got no clue where to start! Please someone help!

Do you mean not with a drag-detector?


One simple way you could do this is add a ball constraint attached to an attachment inside the cart right where you want the character to be (in-between the poles in the front probably). Then, when the prompt is triggered, add an attachment to the character’s HumanoidRootPart and set the BallConstraint to use that attachment.

Then to detach the cart, just delete the attachment created inside the HumanoidRootPart.

The physics engine should automatically give the player network ownership of the cart since the cart would be connected to the player, so that should work well. You can set some of the parts to Massless = true if the cart is too heavy for the player, though I don’t think that should be a problem.


Here is a script because I’m bored and this sounds interesting:

local ballConstraint = -- Set to your ball constraint. Set the .Attachment0 to the attachment in your cart.

local proximityPrompt = script.Parent -- Assuming the script is inside the proximity prompt

local currentAttachment = nil
proximityPrompt.Triggered:Connect(function(triggeredPlayer)
    -- If the current attachment exists in a character in workspace, let that character disconnect it
    if currentAttachment and currentAttachment:IsDescendantOf(workspace) then
        -- Only let the cart player disconnect themselves
        if not currentAttachment.Parent.Parent == triggeredPlayer.Character then return end

        -- Disconnect the cart and remove the attachment
        ballConstraint.Attachment1 = nil
        currentAttachment:Destroy()
        currentAttachment = nil
    else -- Otherwise, let a new player connect to the cart
        -- Make sure the character and hrp exist and are in workspace
        local character = triggeredPlayer.Character
        if not character then return end
        local hrp = character:FindFirstChild("HumanoidRootPart")
        if not hrp then return end
        if not hrp:IsDescendantOf(workspace) then return end

        -- Connect the cart to the character
        currentAttachment = Instance.new("Attachment")
        currentAttachment.Name = "CartAttachment"
        currentAttachment.Parent = hrp
        ballConstraint.Attachment1 = currentAttachment
    end
end)