Make attachment CFrame move up and down with mouse movement

Hello people, I am making a script where there’s a part with drag detector on it and when the player drag the part, it align position with an attachment in front of the humanoid root part and it work perfectly fine.
Then Im trying to make it move up and down on the Y axis when the player is dragging it up and down using the drag continue event and its viewframe, it works but only when looking up and down and it doesn’t depend on the mouse but I want something like this

(The red line is the limit on how high or low the attachment will go)
Anyways, sorry I can’t paste the code here right now as I’m not on my desktop. Thank you in advance

Nvm, ignore all the dragdetector stuff, I want to know how I would just make the attachment move up and down on the Y axis with the mouse movement, the problem is that the mouse is on local script while I have to do the movement on the server

So there’s a few ways of doing this depending on what behaviour you’d want out of this script.

The DragDetector should work, if you utilise the TranslateLine drag-style and the translation limits, though I don’t have much experience with it. Hopefully someone else can contribute some ideas. I’m aware that DragDetectors have a ‘Scriptable’ option which might be worth looking into, but again, I have no experience with it (sorry)

In terms of scripting it:

You could do it really simply, and just track the MousePosition.Y and translate it to a height value of the part between the minimum and maximum, but the position of the part won’t necessarily align with the mouse.

local MinimumHeight = 0;
local MaximumHeight = 5;

local MousePositionY = MousePosition.Y; -- Get from UserInputService or GetMouse() within a RenderStep function

local HeightOfPart = MinimumHeight + (MousePositionY / ViewFrameSize.Y) * (MaximumHeight - MinimumHeight)

If you want it to “follow” the mouse, you could utilise raycasts. An easy way would be to create an invisible wall along the plane you want the part to translate upward/downwards on, then use Camera:ScreenPointToRay to determine a position. Extracting the Y-coordinate of the ray’s intersection point should give you the height value you’re looking for.

There is more complicated ways of doing it without the invisible wall, by doing a ScreenPointToRay and then utilising the :Dot product to determine the intersection point with the plane:

local d = planeNormal:Dot(planePosition - rayOrigin);
local n = planeNormal:Dot(rayDirection);

if (n == 0) then
    return nil -- No intersection (parallel ray);
end

local t = d/n;
if (t < 0) then
    return nil; -- Intersection is behind the ray origin
end

return rayOrigin +rayDirection *t;

But this seems unnecessarily complicated if you can just do the invisible wall.

math.clamp on the resultant Y position will allow you to incorporate your upper and lower limits.

In terms of replicating to the server, you could experiment using SetNetworkOwnership and have the client handle the physics fully independently, or another option may be to use UnreliableRemoteEvents and update the position to the server regularly while it’s being dragged.