Getting the Mouse position within a PluginGui

I am attempting to create a Scroll Bar GUI element within a PluginGui. This requires the mouse position. Normally, I would use GetMouseLocation to get that position, but in PluginGuis this doesn’t work. InputObjects also do not return the correct position.

The only option is to have a GuiObject as a backdrop and use that to get the mouse location. This doesn’t work because people’s mouse cursors will inevitably leave the PluginGui (as happens when scrolling) which means the mouse position will no longer update.

Is there any way to get the mouse position beyond this? I’ve tried using a PluginMouse, and it suffers from the same issue as UserInputService.

I’m purposefully not using ScrollingFrames. They aren’t an option here because I want to have actual arrow buttons that control the frame without it being janky as heck.

3 Likes

edit: check out blob’s post below for a method using InputObjects which should work for you, I was not aware of this functionality!

Old Post

As far as I definitively know, there is no way of doing this. However, I spotted some events on the widget documentation page that suggest some dragging related behaviour. I suspect it’s more to do with drag-and-drop features, but there’s a small chance it could end up being useful;

image

So, you’re probably going to have to live with GuiObjects, unless you’re lucky and Roblox does add functionality to detect mouse drags, but I suspect this is unlikely.

The user presumably clicked on the scroll bar (or something) to start the drag, right? The InputObject when the user clicked will “remain active” until the mouse button is released, which means that the Position property will continue to be updated even if the mouse leaves the window.

Edit: the InputObject corresponding to the MouseButton1 event will not have its Position updated, but an InputObject corresponding to MouseMovement will.

Blue means the button was clicked; yellow means that the mouse is currently not depressed.

local gui = plugin:CreateDockWidgetPluginGui(
	"my-brand!",
	DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float, true, true)
)

local b = Instance.new("TextButton", gui);
b.Size = UDim2.new(.8, 0, .8, 0);
b.AnchorPoint = Vector2.new(.5, .5);
b.Position = UDim2.new(.5, 0, .5, 0);
b.AutoButtonColor = false;

local f = Instance.new("Frame", b);
f.Size = UDim2.new(.1, 0, .1, 0);
f.AnchorPoint = Vector2.new(.5, .5);

local cxn;
b.InputBegan:Connect(function(io)
	--Color the button when the mouse clicks on it.
	if io.UserInputType == Enum.UserInputType.MouseButton1 then
		b.BackgroundColor3 = Color3.fromHSV(.5, 1, 1);
	end

	--Start watching the InputObject when we detect mouse movement over the gui.
	if not cxn and io.UserInputType == Enum.UserInputType.MouseMovement then
		cxn = game:GetService("RunService").Heartbeat:Connect(function()
			b.Text = tostring(io.Position);
			f.Position = UDim2.new(0, io.Position.x - b.AbsolutePosition.x, 0, io.Position.y - b.AbsolutePosition.y);
		end)
	end
end)
b.InputEnded:Connect(function(io)
	if io.UserInputType == Enum.UserInputType.MouseButton1 then
		b.BackgroundColor3 = Color3.fromHSV(1/6, 1, 1);
	end
end)
8 Likes

I hadn’t actually considered that. Thanks!

1 Like