Get Absolute Mouse Position in Roblox Studio

Hello. I’m working on a Plugin. Long story short, I am trying to get the position of the mouse whenever you right-click on a widget, but the positions are relative to the 3D Viewport rather than the full window size.

If there is anyone who knows how to do that, please let me know!

Here is my current code:

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local mouse = player:GetMouse()

local function createDropdownMenu()
	if currentCoreDropdownMenu then currentCoreDropdownMenu:Destroy() currentCoreDropdownMenu = nil end
		
	currentCoreDropdownMenu = script.Dropdown:Clone() -- Frame
	currentCoreDropdownMenu.Parent = newWidget
		
	currentCoreDropdownMenu.Position = UDim2.fromOffset(mouse.X, mouse.Y)
end

I have tried using UserInputService:GetMouseLocation(), but got the same results.

Do you want to get the position as a scale or offset?
I had the exact same problem a few days ago and made this

local relpos = Vector2.new((mouse.X-oldui.AbsolutePosition.X)/oldui.AbsoluteSize.X,(mouse.Y-oldui.AbsolutePosition.Y)/oldui.AbsoluteSize.Y)
newui.Position = UDim2.new(UDim.new(relpos.X,0),UDim.new(relpos.Y,0))
1 Like

If that doesn’t work try with the PluginMouse

1 Like

Sorry for the late reply.

I need the values as Offset.

Is the oldui a Frame? I don’t have two separate UIs so I’m kinda confused on how to set this up.

Sorry, what is the difference between LocalPlayer:GetMouse() and plugin:GetMouse()?

They both seem to be getting values relative to the 3D Viewport rather than the full window size.

I updated my code to this:

local mouse = plugin:GetMouse()

local function createDropdownMenu()
		if currentCoreDropdownMenu then currentCoreDropdownMenu:Destroy() currentCoreDropdownMenu = nil end
		
		currentCoreDropdownMenu = Instance.new("ScreenGui")
		currentCoreDropdownMenu.IgnoreGuiInset = true
		currentCoreDropdownMenu.DisplayOrder = 999999
		currentCoreDropdownMenu.Parent = CoreGui
		
		local frame = script.Dropdown:Clone()
		frame.Parent = currentCoreDropdownMenu
		
		print(mouse.X, mouse.Y) -- 0 Y value
		frame.Position = UDim2.fromOffset(mouse.X, mouse.Y)
	end

Since my widget is on the bottom left of your screen, whenever your mouse leaves the 3D viewport the X value becomes zero.

Am I doing something wrong? Please tell me.

Then that means plugins cannot access the mouse if they’re not in the focus of their DockWidget or the Viewport. It might be a studio limitation or a security prevention

Then is there any way to get the absolute mouse position?

To give you more context, I am working on a chat plugin like Discord. Whenever you right-click on a message, a dropdown with some options (like Delete Message, Edit Message, Report Message etc.) should open.

All of that works, except the position. Is there any way you would be able to get the position of your mouse inside of a widget? That would be useful too.

Chat plugin? Message? That means you have your own UI, either in the Viewport or in a DockWidget? Why not simply use a button?
So you’re using PluginMouse, a DockWidget, but the location is still 0,0?

The location is 0 on the X since it is locatedat the bottom left corner. The mouse position just stops updating after you focus on a DockWidget.

I am using plugin:GetMouse() and checking the X and Y values once I clone the frame, as you can see from the code.

Bumping because I still need help.

Can you try GetRelativeMousePosition on the widget itself?

1 Like

Thank you. That is exactly what I have been looking for! :slightly_smiling_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.