PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
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.

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 function moveGuiToMouse()
	local mouseLocation = UserInputService:GetMouseLocation()
	gui.Position = mouseLocation
end

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