Dragging Frames to Mouse

So I’m trying to create a Drag and Drop inventory system. It’s going well so far, but there is a problem when dragging the frame.
Image from Gyazo

I want to make it so it drags from the position I clicked. However, the mouse just goes to the corner of it since I’m simply using this to set the position.

itemLabel.Position = UDim2.new(0, mouse.X, 0, mouse.Y)

The only solution I can think of is setting the Anchor Point to my mouse position, but since it uses scale I’m not sure how to convert my mouse position offset into a percentage. I’m not even sure if changing the Anchor Point will fix it, so please tell me some other solutions. If you’re confused on what I’m trying to do here is an example:
SmoothMenu
You’ll see how no matter where the mouse is clicked, the frame moves with that position. But in mine, it moves from the corner no matter what.

2 Likes

Try changing the text labels anchor point to 0.5,0.5 hope this helps!

Just read the topic again I believe that game uses math to calculate the offset of the position so try fiddling around with the positioning and offset

Have you tried to get the mouses last position, then adding / subtracting the frames position from that value?

It gets bigger because you are using the Scale property adjusted to the frame that the frame is a member of. Try reducing the size while it’s being dragged to avoid that enlargement, then change it back once dropped.

Yeah, I was gonna fix that after I finished this problem

Could you explain a bit more and no I haven’t

I’m assuming you are using a loop / render stepped for this. You can create an offset value by subtracting the mouses initial position by the frames first position. You can then apply this offset to the frames position everytime it is moved.

I’m still a bit confused but I’ll try it out. something like mouse.X - frame.AbsolutePosition.X?? I doubt that’s what you’re trying to say

Subtract the mouse x by the highest x size value of the text label and same goes for z

Set the anchor point of the object to 0.5, 0.5 (or keep it at 0, 0 if its kind of off, and use this code:

local SelectPosition = Mouse.Position - itemLabel.Position -- GUI is the object clicked

itemLabel.Position = UDim2.new(0, mouse.X, 0, mouse.Y) + SelectPosition

Basically what this does is it offsets the position values based off of where it is clicked.

1 Like

Mouse.Position isn’t a thing, what should I use instead of this? And should I subtract and X and Y separately?

Yeah probably 30 charsssssssssss