PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetMouseLocation

This function returns a DataType/Vector2 representing the current screen location of the player’s Mouse in pixels relative to the top left corner. This does not account for the GuiObject|GUI inset.

If the location of the mouse pointer is offscreen or the players device does not have a mouse, the value returned will be undetermined instead of Vector2.

As UserInputService is client-side only, this function can only be used in a LocalScript.

Returns

Return Type Summary

A DataType/Vector2|Vector2 representing the current screen location of the mouse, in pixels


Code Samples


Move GUI To Mouse Location

This example binds the moveToMouse() to RunService/BindToRenderStep|RunService's RenderStep to move the GuiObject|GUI to the location of the player’s mouse. It does this by converting the location, a datatype/Vector2, into a datatype/UDim2.

The example sets the value of the GUI’s parent ScreenGui ScreenGui/IgnoreGuiInset property to false force the GUI Inset imposed by Roblox’s CoreGuis to be ignored by the ScreenGui and its descendants

In order for this example to work as expected, it should be placed in a LocalScript that is a child of the GUI being moved to the mouse’s location.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local gui = script.Parent
local screenGui = gui.Parent
screenGui.IgnoreGuiInset = true
mouseLocation = UserInputService:GetMouseLocation()
gui.Position = UDim2.fromOffset(mouseLocation.X, mouseLocation.Y)
local function moveGuiToMouse()
	mouseLocation = UserInputService:GetMouseLocation()
	gui.Position = UDim2.fromOffset(mouseLocation.X, mouseLocation.Y)
end

RunService:BindToRenderStep("moveGuiToMouse", 1, moveGuiToMouse)