Move Frame Towards my Mouse Position

I am trying to set the frames Position to my mouse’s current position. But I am getting an error while doing so. Here is my current code.

for _, buttons in ipairs(script.Parent:GetDescendants()) do
	if buttons:IsA('TextButton') or buttons:IsA('ImageButton') then
		buttons.MouseButton1Click:Connect(function()
			CircleEffect.Position = mouse.Hit.Position
		end)
	end
end

Any help will be apperciated.

What’s the error you get? Is CircleEffect a part or a ui object?

it is a gui object.

[charrrrrs]

mouse.Hit.Position is a Vector3, UIObject.Position is a UDim2, since you can’t convert a Vector3 to a UDim2, you’d use mouse.X and mouse.Y and do this;

for _, buttons in ipairs(script.Parent:GetDescendants()) do
	if buttons:IsA('TextButton') or buttons:IsA('ImageButton') then
		buttons.MouseButton1Click:Connect(function()
			CircleEffect.Position = UDim2.new(0, mouse.X, 0, mouse.Y);
		end)
	end
end

Ok I tried the code, and yes the ui moves, but the circle is moving off my screen? Not sure whats going on.

Can you send a video of what’s happening?

The greyish circle is the effect I referenced in the script

What’s the circle parented to? The circle’s position relative to it’s parent UI.

The Circle is a frame parented to a ScreenGui

When you click the button, go to the explorer > Players > You > PlayerGui > ScreenGui > Circle Frame and check what position it went to

this is the position {557, 0},{581, 0}

Oh, you put mouse.X and mouse.Y in the wrong places, rearrange it to what I put; UDim2.new(0, mouse.X, 0, mouse.Y);

Yes it works! In order to align it perfectly with my mouse, would i have to work with offsets and stuff like that?

Since the position is based on the top left-most pixel, you’ll add half of the frame’s size to the final position.

Whoops, you’ll want to subtract the frame’s size, not add it.

CircleEffect.Position = UDim2.new(0, mouse.X - CircleEffect.AbsoluteSize.X/2, 0, mouse.Y - CircleEffect.AbsoluteSize.Y/2);
3 Likes