Help with Gui, make button follow mouse

So let’s say I have a frame with a button in it. I want to make it so the button follows the player’s mouse. Only problem is, well if you know how gui positioning is then you know this might be a problem.

If the frame is 50% of the screen then this is going to change where said button goes too considering when using scale it goes between 0 and 100%

So my question is how can I get it to follow the mouse without it getting messed up by its parent or whatever

Are you meaning something like this? You can calculate the bounds of the frame and button then use math.clamp to keep the button within the frame.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local RunService = game:GetService("RunService")
local GUI = script.Parent
local Frame = GUI.Frame
local Button = GUI.TextButton

local Frame_Left_Bound = (Frame.Position.X.Scale - (Frame.Size.X.Scale / 2)) * GUI.AbsoluteSize.X
local Frame_Right_Bound = (Frame.Position.X.Scale + (Frame.Size.X.Scale / 2)) * GUI.AbsoluteSize.X
local Frame_Top_Bound = (Frame.Position.Y.Scale - (Frame.Size.Y.Scale / 2)) * GUI.AbsoluteSize.Y
local Frame_Bottom_Bound = (Frame.Position.Y.Scale + (Frame.Size.Y.Scale / 2)) * GUI.AbsoluteSize.Y

local Button_Width = Button.Size.X.Offset / 2
local Button_Height = Button.Size.Y.Offset / 2

Button.Position = UDim2.fromOffset(Button.Position.X.Scale * GUI.AbsoluteSize.X, Button.Position.Y.Scale * GUI.AbsoluteSize.Y)

RunService.RenderStepped:Connect(function()
	Button.Position = UDim2.fromOffset(
		math.clamp(
			Mouse.X, 
			Frame_Left_Bound + Button_Width, 
			Frame_Right_Bound - Button_Width), 
		math.clamp(
			Mouse.Y, 
			Frame_Top_Bound + Button_Height, 
			Frame_Bottom_Bound - Button_Height)
		)
end)

2 Likes

This isnt what I wanted at first but I might use this after all! I must conduct some more tests first

1 Like

Ok so I finally got to trying this, but It doesnt really work for me. It probably has to do with the fact that the frame Im trying to move is not just in a frame, its in a frame thats in a frame thats in a frame, it also always gives me the same position every time. Heres my code

Connections['ClickEnter'] = frame:FindFirstChild('TIme'):FindFirstChild('Time'):FindFirstChild('Time'):FindFirstChild('DetectClick').MouseButton1Click:Connect(function()

	print('Clicked')
	local z = script:FindFirstChild('TimeTag'):Clone()
	

	-- get stuff

	local Frame = frame:FindFirstChild('TIme'):FindFirstChild('Time'):FindFirstChild('Time')
	z.Parent = frame

	local Frame_Left_Bound = (Frame.Position.X.Scale - (Frame.Size.X.Scale / 2)) * frame.AbsoluteSize.X
	local Frame_Right_Bound = (Frame.Position.X.Scale + (Frame.Size.X.Scale / 2)) * frame.AbsoluteSize.X
	local Frame_Top_Bound = (Frame.Position.Y.Scale - (Frame.Size.Y.Scale / 2)) * frame.AbsoluteSize.Y
	local Frame_Bottom_Bound = (Frame.Position.Y.Scale + (Frame.Size.Y.Scale / 2)) * frame.AbsoluteSize.Y


	local Button_Width = z.Size.X.Offset / 2
	local Button_Height = z.Size.Y.Offset / 2

	z.Position = UDim2.fromOffset(z.Position.X.Scale * frame.AbsoluteSize.X, z.Position.Y.Scale * frame.AbsoluteSize.Y)


	z.Position = UDim2.fromOffset(
		math.clamp(
			mouse.X, 
			Frame_Left_Bound + Button_Width, 
			Frame_Right_Bound - Button_Width), 
		math.clamp(
			mouse.Y, 
			Frame_Top_Bound + Button_Height, 
			Frame_Bottom_Bound - Button_Height)
	)
	print(z.Position)
end)

It may be because the code is only running once when you click, the code i provided should be in a a loop. So try connecting to a RunService loop when you click and disconnect when neccessary.
Also try removing the first z.Position = UDim2.fromOffset(...). That was just for debugging it should work without.