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

Mouse Pointer Control

Mouse Pointer Control

Sep 25 2019, 2:01 PM PST 5 min

This article explains how the mouse pointer behavior can be customized.

Set Mouse Icon

Changing the icon of a player’s mouse can be done in a LocalScript by setting its Mouse/Icon|Icon to a custom Roblox asset ID:

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "rbxassetid://3400146391"

Note that since the icon is set in a LocalScript, each player can have a different icon, or can have an icon appear in certain situations like when an ability is recharging or the mouse is hovering over a hostile target.

Hide Mouse Icon

The mouse icon’s visibility can be toggled on and off via UserInputService/MouseIconEnabled within a LocalScript. For example, the following code will switch the mouse from visible to invisible and back every two seconds:

local UserInputService = game:GetService("UserInputService")

while true do
	wait(2)
	UserInputService.MouseIconEnabled = false
	wait(2)
	UserInputService.MouseIconEnabled = true
end

Lock Mouse Position

The mouse position can be locked to the screen using UserInputService/MouseBehavior with a value of enum/MouseBehavior|Enum.MouseBehavior.LockCurrentPosition or enum/MouseBehavior|Enum.MouseBehavior.LockCenter. Setting this property back to enum/MouseBehavior|Enum.MouseBehavior.Default will unlock the mouse.

local UserInputService = game:GetService("UserInputService")

wait(1)

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition

UserInputService.InputChanged:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		print("Mouse delta is (" .. tostring(inputObject.Delta.X) .. ", " ..  tostring(inputObject.Delta.Y) .. ")")
	end
end)

Note that if the mouse is locked, UserInputService/InputChanged will still fire when the player moves the mouse, passing in the distance the mouse has moved.

Tags:
  • input
  • mouse
  • ui
  • gameplay