Custom Mouse Cursor

Hello, in the project I’m working on I need a dot as the mouse cursor (the project will be in first-person). I’ve tried multiple solutions, but none work fully, as the mouse cursor sometimes switches back and forth to the default if I point it at anything clickable like GUIs.
How would I make a mouse cursor that doesn’t change at all and stays the same as I want it?

5 Likes

You can hide the mouse icon and use an Image Label as a mouse icon.
First you need to hide the mouse icon like this:

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

-- Hide the default mouse cursor
UserInputService.MouseIconEnabled = false

Then you should continuously update the position of the Image Label to match the mouse movement:

local ImageLabel = Path.to.your.Imagelabel 
game:GetService("RunService").RenderStepped:Connect(function()
    ImageLabel.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
end)
5 Likes

Have tried that exactly, but still switches for a split second back to the default as I said here.

2 Likes

Have you tried changing the mouse icon?

local Mouse = Player:GetMouse()
Mouse.Icon = "http://www.roblox.com/asset?id=[YOUR ID HERE]"
1 Like

Yes of course. Although just to double check, I will try both solutions another time, and I’ll tell you what problems I face

robloxapp-20240907-1533390.wmv (106.5 KB)
First script, as I stated will change to the default for a split second when hovering over clickable GUIs.

Edit: I dont know how to not make the vid a link, it just downloads the vid for you
Same thing happens when I make the Mouse.Icon the ID I want it to be.

I do not see something wrong in the video, the icon is the same

Just checked the video, the quality is horrendous sorry about that, but in studio it does switch to the normal mouse icon for some reason. This might be a studio bug, so I’ll try this out on roblox and see how it goes

Alright and let me know if something new happen

1 Like

Okay it seems to be a studio bug because it works perfectly on roblox.
Btw I’m using a different code that I found somewhere, here it is if anyone needs it:

local UIS = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local plrGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local guiInset = GuiService:GetGuiInset()
local icon = script.Parent:WaitForChild("Cursor")

local MouseIcons = {"rbxassetid://14691803466","rbxassetid://14691803466","rbxassetid://12323391502"}
--Rest, Hover, Typing

RunService.RenderStepped:Connect(function(dt)
	UIS.MouseIconEnabled = false
	local mousePos = UIS:GetMouseLocation() - guiInset
	icon.Position = UDim2.fromOffset(mousePos.X,mousePos.Y)
	local uiObjects = plrGui:GetGuiObjectsAtPosition(mousePos.X,mousePos.Y)
	local iN = 1
	local z = -1000
	for _,obj:GuiObject in ipairs(uiObjects) do
		if obj:IsA("GuiButton") and obj.Active == true then
			if obj.ZIndex > z then
				z = obj.ZIndex
				iN = 2
			end
		elseif obj:IsA("TextBox") and obj.Active == true then
			if obj.ZIndex > z then
				z = obj.ZIndex
				iN = 3
			end
		end
	end
	icon.Image = MouseIcons[iN]
end)

Thanks for your help!

1 Like

Oh hey there, sorry to bother you again but I found another issue while testing on mobile: the mouse icon moves to place where I touch the screen. This is a problem because it’s a first-person game and I don’t want the mouse icon to move away from the middle. Do you know of any solution?

This is the issue, so when ever you tap on the screen it will be moving the image label to the mouse position. On PC when in first person the mouse is locked to the center of the screen but on mobile any time you touch it will update the mouse pos x and y values.

instead just set it to the center of the screen

UDim2.new(0.5, 0, 0.5, 0)

How would I unlock it then whenever I’m in cutscenes or anything like that?

1 Like

Connect that part inside a RenderStepped when you want to unlock:

icon.Position = UDim2.fromOffset(mousePos.X,mousePos.Y)

and for locking just disconnect it, and set the position to UDim2.new(0.5, 0, 0.5, 0) as @PlanetCaravn said

1 Like

alright Ill try it out and see how it goes

1 Like

Alright, I made this system for unlocking and locking the mouse, but I still have issues. For example, if I have it unlocked from the start and I lock it through the command bar, it disconnects the connection but on the screen its still running, it only puts it in the middle for 1 frame.
And it’s really weird cause this doesn’t happen if I do it through a script, and if I do it through a script the command bar starts correctly locking and unlocking again? I’m confused, here’s the script:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local connection

local CustomCursor = {}

local mouse = Players.LocalPlayer:GetMouse()
UserInputService.MouseIconEnabled = false

local icon = script.Parent.Cursor
icon.Visible = true

local function unlock()
	 print("unlocked")
	connection = RunService.RenderStepped:Connect(function()
		local mousePos = UserInputService:GetMouseLocation()
         icon.Position = UDim2.fromOffset(mousePos.X, mousePos.Y)
    end)
end

local function lock()
    print("locked")
	if connection then
		print("theres a connection")
		connection:Disconnect()
        connection = nil
		print(connection)
    end
	icon.Position = UDim2.new(0.5, 0, 0.5, 0)
	print(icon.Position)
end

function CustomCursor:Lock()
    lock()
end

function CustomCursor:Unlock()
    unlock()
end

return CustomCursor 

I have set MouseBehavior to LockCenter and after disconnecting you probably wouldn’t need to set connection to nil. (yeah and i added the MouseBehavior in a loop incase the control scripts are setting MouseBehavior back)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local connection
local connection2

local CustomCursor = {}

local mouse = Players.LocalPlayer:GetMouse()
UserInputService.MouseIconEnabled = false

local icon = script.Parent.Cursor
icon.Visible = true

local function unlock()
	print("unlocked")
	if connection2 then
		connection2:Disconnect()
	end
	connection = RunService.RenderStepped:Connect(function()
		local mousePos = UserInputService:GetMouseLocation()
		icon.Position = UDim2.fromOffset(mousePos.X, mousePos.Y)
	end)
end

local function lock()
	print("locked")
	if connection then
		print("theres a connection")
		connection:Disconnect()
		print("no connection")
	end
	connection2 = RunService.RenderStepped:Connect(function()
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	end)
	icon.Position = UDim2.new(0.5, 0, 0.5, 0)
	print(icon.Position)
end

function CustomCursor:Lock()
	lock()
end

function CustomCursor:Unlock()
	unlock()
end

return CustomCursor

1 Like

And now if I lock it I can’t unlock it. I’m really confused, roblox has terrible mouse customization stuff

I’ve further tesetd the one I made, and it seems to work perfectly fine except if I use it with the command bar. Super weird, but it works. I’ll let ya know if I run into more issues, thanks for sticking through lol

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.