PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

MouseMoved

Fires whenever a user moves their mouse while it is inside a GuiObject|GUI element. It is similar to Mouse/Move, which fires regardless whether the user’s mouse is over a GUI element.

Note, this event fires when the mouse’s position is updated, therefore it will fire repeatedly whilst being moved.

The x and y arguments indicate the updated screen coordinates of the user’s mouse in pixels. These can be useful to determine the mouse’s location on the GUI, screen, and delta since the mouse’s previous position if it is being tracked in a global variable.

The code below demonstrates how to determine the DataType/Vector2 offset of the user’s mouse relative to a GUI element:

local CustomScrollingFrame = script.Parent
local SubFrame = CustomScrollingFrame:FindFirstChild("SubFrame")

local mouse = game.Players.LocalPlayer:GetMouse()
function getPosition(X, Y)
	local gui_X = CustomScrollingFrame.AbsolutePosition.X
	local gui_Y = CustomScrollingFrame.AbsolutePosition.Y
	
	
	local pos = Vector2.new(math.abs(X - gui_X), math.abs(Y - gui_Y - 36))
	print(pos)
end

CustomScrollingFrame.MouseMoved:Connect(getPosition)

Note that this event may not fire exactly when the user’s mouse enters or exits a GUI element. Therefore, the x and y arguments may not match up perfectly to the coordinates of the GUI’s edges.

See also

  • GuiObject/MouseEnter
  • GuiObject/MouseLeave
  • GuiObject/MouseWheelForward
  • GuiObject/MouseWheelBackward

Parameters

Name Type Default Description

x

int

The mouse’s x screen coordinate in pixels, relative to the top left corner of the screen

y

int

The mouse’s y screen coordinate in pixels, relative to the top left corner of the screen


Code Samples


Drawing Canvas GUI

The code sample below creates a GUI Frame that acts as a canvas that can be drawn on and cleared.

It uses two UserInputService events, UserInputService/InputBegan and UserInputService/InputEnded to determine when the user’s left mouse button is down. These events set the hold variable so that the paint function only draws a new pixel if the user’s left mouse button is pressed.

When the user’s mouse moves on the canvas, the pointer GUI moves to follow the mouse and, if the hold is true, clones the pointer to draw a new pixel onto the canvas at the pointer’s location. The pointer’s visibility is set to false when the mouse exits the GUI and to true when the mouse enters the GUI.

Pressing the clear button destroys all Canvas children named “Pixel” to clear the canvas.

Canvas GUI Demonstration

Gui in Studio Explorer

-- Services
local UserInputService = game:GetService("UserInputService")

-- GUI Elements
local Canvas = script.Parent
local ClearButton = script.Parent.Parent:FindFirstChild("ClearButton")
local pointer = Canvas:FindFirstChild("Pointer")

-- Player's Mouse
local mouse = game.Players.LocalPlayer:GetMouse()

-- Global boolean indicating whether player's MouseButton1 is down
local hold = false

function paint(X, Y)	
	local gui_X = Canvas.AbsolutePosition.X
	local gui_Y = Canvas.AbsolutePosition.Y
		
	local offset = Vector2.new(math.abs(X - gui_X), math.abs(Y - gui_Y - 36))
	pointer.Position = UDim2.new(0, offset.X, 0, offset.Y)
	
	if hold == false then return end
		
	local pixel = pointer:Clone()
	pixel.Name = "Pixel"
	pixel.Parent = Canvas
end

function clear()
	local children = Canvas:GetChildren()
	
	for i, child in pairs (children) do
		if child.Name == "Pixel" then
			child:Destroy()
		end
	end
end

function showPointer()
	pointer.Visible = true
end

function hidePointer()
	pointer.Visible = false
end

function inputBegan(input)
	local inputType = input.UserInputType
	  if inputType == Enum.UserInputType.MouseButton1 then
        hold = true
    end
end

function inputEnded(input)
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = false
	end
end

-- Events
ClearButton.MouseButton1Click:Connect(clear)

UserInputService.InputBegan:Connect(inputBegan)
UserInputService.InputEnded:Connect(inputEnded)

Canvas.MouseMoved:Connect(paint)
Canvas.MouseEnter:Connect(showPointer)
Canvas.MouseLeave:Connect(hidePointer)