Draggable Parts

I tried making a basic draggable and droppable part script, all I wanted was for the player to be able to hover over a model and when the player pressed left click and held it the item would be able to be repositioned/ I wanted the player to be able to drop the part after letting go of left click. I did a TON of research but there doesn’t seem to be a lot about this specific topic, I wrote a script and the output has no errors. I at first thought it was because it was unanchored but that didn’t make sense because I had seen videos of other people doing it and the part appeared to be anchored. I’d be more than happy to send the script so people can critique it but, as of now I have no idea why the script won’t work.

1 Like

Yes please add the script so we can understand

There is a Couple of ways you can do this, I’ll go over one:

You would Need an ObjectValue as the way to know what Part you have.
You would then use the Client to detect the Mouse Hit Position, they way you do this is:

local Player = game.Players.LocalPlayer -- Player LocalScript is running for
local Mouse = Player:GetMouse() -- Gets Players Mouse

Once you do that, detect if their Mouse Button was Clicked using Mouse.Button1Down, once done you would ray cast using workspace:Raycast() to get the Part that is intersected, you can make sure to Filter Parts that you don’t want to be Dragged (You don’t want people destroying your game do you?), you can then get the Detected part, Add it to the ObjectValue so it will stay Connected, and then you could drag it around by setting its Position to either Mouse.Hit.p, or you could ray cast again, and use Raycast.Position + Raycast.Normal to have the Part not clip through Objects.

If can then use Mouse.Button1Up to detect if the Player let go of the Part, or use the Mouse.Button1Down Connection to check if a bool is true or false, you can let the part go, Basic Example here:

local Active = false
Mouse.Mouse1Down:Connect(function()
    if not Active then -- if false or nil
        Active = true
    else
        Active = false
    end
end)

If you want a Client Change, let this be, but if you want a Server change, then you would be using RemoteEvents for this occasion, since you cant use LocalPlayer data due to the Server not knowing what (or who) the Player is, we can just send that Data to the Server so the Server knows what to do with that Info.

I got this set up rn, I made changes to it and it “sorta works” I say sorta because it does infact pick up the park but, the part just kinda stick to the floor, also when I put something above me like another part, it will just stick to that instead of falling directly down like I want it to

Script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local target
local down

mouse.Button1Down:connect(function()
if mouse.Target ~= nil and mouse.Target.Locked == false then
target = mouse.Target
mouse.TargetFilter = target
down = true
end
end)

mouse.Move:Connect(function()
if down == true and target ~= nil then
target.Position = mouse.Hit.Position
end
end)

mouse.Button1Up:connect(function()
down = false
mouse.TargetFilter = nil
target = nil
end)