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

MouseEnter

The MouseEnter event fires when a user moves their mouse into a GuiObject|GUI element.

Please do not rely on the x and y arguments passed by this event as a fool-proof way to to determine where the user’s mouse is when it enters a GUI. These coordinates may vary even when the mouse enters the GUI via the same edge - particularly when the mouse enters the element quickly. This is due to the fact the coordinates indicate the position of the mouse when the event fires rather than the exact moment the mouse enters the GUI.

This event fires even when the GUI element renders beneath another element.

If you would like to track when a user’s mouse leaves a GUI element, you can use the GuiObject/MouseLeave event.

See also

  • GuiObject/MouseLeave
  • GuiObject/MouseMoved
  • 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)

Printing where a Mouse Enters a GuiObject

The following example prints the mouse location, in pixels, when it enters GUI element.

GuiObject.MouseEnter:Connect(function(x, y)
	print("The user's mouse cursor has entered the GuiObject at position (" .. x .. ", " .. y .. ").")
end)