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.
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
y arguments may not match up perfectly to the coordinates of the GUI’s edges.
The mouse’s x screen coordinate in pixels, relative to the top left corner of the screen
The mouse’s y screen coordinate in pixels, relative to the top left corner of the screen
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/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.