Mouse Appearance

This tutorial explains how the appearance of the mouse can be customized. The examples given in this tutorial must be executed in LocalScripts to work.

Set mouse icon[edit]

Changing the icon of a player's mouse is simply done in a LocalScript. The icon has to be a Decal and can be changed as often as you like.

Select
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset?id=163023520"

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

Hide mouse icon[edit]

The mouse icon's visibility can be toggled on and off with MouseIconEnabled. This function must be called in a LocalScript.

For example, the following code, when put in StarterGui, will switch the mouse from visible to invisible and back every two seconds:

Select
local UserInputService = game:GetService("UserInputService")
 
while true do
	wait(2)
	UserInputService.MouseIconEnabled = false
	wait(2)
	UserInputService.MouseIconEnabled = true
end

Lock mouse position[edit]

The mouse position can be locked to the center of the screen using MouseBehavior. Setting this property to Enum.MouseBehavior.LockCenter will lock the mouse to the center of the screen, Enum.MouseBehavior.Default will unlock the mouse.

If the mouse is locked, InputChanged will still fire when the player moves the mouse and will pass in the distance the mouse has moved.

Select
local UserInputService = game:GetService("UserInputService")
 
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		print("delta is (" .. tostring(inputObject.Delta.x) .. ", " ..  tostring(inputObject.Delta.y) .. ")")
	end
end)

Example[edit]

Using MouseIconEnabled and MouseBehavior allows for creating a custom camera system that moves the camera simply when the player moves their mouse left and right:

Select
-- Lock mouse to center of the screen and hide it
local UserInputService = game:GetService("UserInputService")
 
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
 
-- Change the player's camera to scriptable so it can be moved with code
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
 
-- Bind function to when the mouse moves. If it moves left or right, adjust an angle variable which will be used later
local angle = 0
UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		if inputObject.Delta.x < 0 then
			angle = angle - 5
		elseif inputObject.Delta.x > 0 then
			angle = angle + 5
		end
	end
end)
 
-- On renderstepped move the camera around the player based on the angle variable declared above
game:GetService("RunService").RenderStepped:connect(function()
	local x = 10 * math.cos(math.rad(angle))
	local z = 10 * math.sin(math.rad(angle))
	local camPosition = Vector3.new(x, 5, z)
	local torsoPosition = game.Players.LocalPlayer.Character.Torso.Position
	camPosition = camPosition + torsoPosition
	camera.CoordinateFrame = CFrame.new(camPosition, torsoPosition)
end)