Mouse

Mouse : Instance

Overview

The Mouse class offers a range of properties and methods for interacting with the player's mouse. It is often used for tasks such as detecting where the mouse is pointing in the game world, changing the appearance of the cursor, or implementing click-based mechanics.

Description

The Mouse class provides developers with tools to interact with the player's mouse input. It is primarily used to detect the mouse's position, raycast into the 3D world, and handle mouse-related events like clicks. The Mouse object is accessible via Player:GetMouse() and is commonly used in local scripts for gameplay mechanics or UI interactions. However, newer projects are encouraged to use UserInputService for more robust input handling.

Properties

Hit

CFrame

Hit represents the CFframe of an object in the 3D space that the mouse is pointing to.

Code Samples

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function UpdateEvent(deltaTime)
    print("Hit Position : ", Mouse.Hit.Position)
end
RunService.Heartbeat:Connect(UpdateEvent)

Origin

CFrame

Origin represents the CFrame of the mouse's position in 3D space relative to the Workspace and is positioned at the Workspace.CurrentCamera oriented toward the Mouse's 3D position.

Code Samples

//NotWork//

ViewSizeX

number

ViewSizeX represents the width of the game window in pixels

Code Samples

//NotWork//

ViewSizeY

number

ViewSizeY represents the height of the game window in pixels

Code Samples

//NotWork//

X

number

The horizontal position of the cursor on the screen.

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local RunService = game:GetService("RunService")
local function UpdateEvent(deltaTime)
    print("Mouse Position : ", Mouse.X, " / ", Mouse.Y) -- ํ™”๋ฉด ์ขŒํ‘œ ์ถœ๋ ฅ
end
RunService.Heartbeat:Connect(UpdateEvent)

Y

number

The vertical position of the cursor on the screen.

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local RunService = game:GetService("RunService")
local function UpdateEvent(deltaTime)
    print("Mouse Position : ", Mouse.X, " / ", Mouse.Y) -- ํ™”๋ฉด ์ขŒํ‘œ ์ถœ๋ ฅ
end
RunService.Heartbeat:Connect(UpdateEvent)

Methods

Events

Button1Down

This event is triggered when the primary mouse button (Button1) is pressed down. It is commonly used to detect when the player initiates a click action in the game. Developers can connect custom logic to this event for various gameplay mechanics, such as interacting with objects, selecting items, or performing attacks.

Parameters

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function OnButton1Down()
    print("Button1Down", Mouse.Target)
end
Mouse.Button1Down:Connect(OnButton1Down)

Button1Up

Button1Up represents the event triggered when the primary mouse button (Button1) is released. It is typically used to detect when a player finishes a click action in the game. This event can be utilized for gameplay interactions like ending a drag-and-drop sequence, confirming selections, or stopping an ongoing action triggered by a mouse press.

Parameters

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function OnButton1Up()
    print("Button1Up")
end
Mouse.Button1Up:Connect(OnButton1Up)

Button2Down

This event is triggered when the secondary mouse button (Button2) is pressed down. It is generally used to detect right-click actions in the game and is commonly utilized for mechanics such as opening context menus, rotating objects, or providing additional interaction options.

Parameters

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function OnButton2Down()
    print("Button2Down", Mouse.Target)
end
Mouse.Button2Down:Connect(OnButton2Down)

Button2Up

Button2Up represents the event triggered when the secondary mouse button (Button2) is released. It is commonly used to detect when a player finishes a right-click interaction in the game. This event can be utilized for various gameplay scenarios, such as finalizing a context menu, stopping a rotation action, or ending any secondary mouse-triggered tasks.

Parameters

Code Samples

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function OnButton2Up()
    print("Button2Up")
end
Mouse.Button2Up:Connect(OnButton2Up)

TouchStarted

TouchStarted Event Description This event is triggered when a touch input begins, such as when the player places a finger on a touch-enabled device. It can be used to implement touch-based interactions, such as tapping on objects or starting a drag action.

Parameters

Code Samples

TouchEnded

The TouchEnded event is triggered when a touch input ends, such as when the player lifts their finger off a touch-enabled device. This event is particularly useful for handling the end of touch interactions, such as releasing a button, dropping an object, or confirming a selection in touch-based gameplay mechanics.

Parameters

Code Samples