Problems With Gui Following Mouse

So I want to make this script so when you hold down on a gui it follows the position of the mouse. The problem is when I tried this with taking the main position and offsetting it, it has another little offset when it tries following my mouse.

local origpos = obj.Box.AbsolutePosition

repeat
    local desiredPos = Vector2.new(mousepos.X,mousepos.Y)
    local relativePosition = desiredPos - origpos
    
    obj.Box.Position = UDim2.fromOffset(relativePosition.X,relativePosition.Y)
    wait()
until down == false

2 Likes

Try setting the ScreenGui’s IgnoreGuiInset property to true.

1 Like

That didn’t change anything, isn’t it just supposed to make the gui ignore the top bar?

TargetFilter perhaps and set the Gui’s boundaries to filter anything outside of for instance: Frame.Size.Y - Frame.Size.X blah blah

Also, I would set the zIndex > than the Frame’s zIndex so those Buttons look cleaner :wink:

1 Like

Forgive me if I don’t understand, but I’m trying to make the gui directly follow the mouse, and I don’t think using TargetFilter would do that.

Also thanks for pointing out that zIndex problem!

1 Like

I meant it as a suggestion for keeping the frames inside the main frame.

Raise the ZIndex to be higher than the frame you’re dragging the letters to.

1 Like

That won’t work unless it applies some ClipDescendants logic like I suggested.

Very simple.

Just write this

local Player = game.Players.LocalPlayer
local mouse = player:GetMouse()
-- now anchor your gui object 0.5,0.5 this is makes it more simple to center it.

guiObject.Position = Udim2.new(0, mouse.X - "parent of guiObject".AbsolutePosition.X, mouse.Y - "parent of guiObject".AbsolutePosition.Y, -20 -- to offset it 20 pixels above the mouse icon.)

That’s how you center it as for holding down on the gui just use math.clamp to stay within the bounds of the frame and you should be good.

4 Likes

The gui can already follow the mouse, it just has an offset. Also I tried that and it does the exact same thing. Is the offset something to do with the UIGridLayout and other guis?

Also, I don’t need it to stay within the frame, just to follow the mouse.

If you tried the example I gave then it probably has something to do with the uigridlayout because to follow the mouse the code I gave should work. If it’s doing the exact same thing like you stated then delete the grid layout is all I can say.

I figured out that if I clone it and set its parent to screengui while maintaining the absolute size, I can set the position of the mouse. For some reason I had to subtract 0.075 scale to make it centered.

1 Like

Just solved my issue 2 years later

1 Like