How would I make a custom cursor for when you hover over a part with a clickdetector?

Hey everyone! So this is sort of a non issue. Its not too “game changing” but I’d like to know how I would make it so when you hover your cursor over a part with a clickdetector inside of it it changes to a different icon when a custom cursor is being used.

This is what I am looking for.

But when a custom cursor is applied…

I’d like to know how to make it so the icon changes like in the first video. I’ve already tried the cursoricon property in the clickdetector but it doesn’t change anything. Is there an easier way to change this instead of using MouseHoverEnter and making a table with all my clickdetectors?

Thanks!

4 Likes

There’s CursorIcon propetry of ClickDetector. I didn’t use it but I guess it should work just perfect.

2 Likes

Is your cursor being set on loop? If it is, use this in a LocalScript:

icon = "your icon"
game.Players.PlayerAdded:Connect(function(player)
	local mouse = player:GetMouse()
	mouse.Icon = icon
end)

Then, you should be able to use CursorIcon.

2 Likes

He already references that he’s attempted to use CursorIcon, but to no avail.

2 Likes

Oh, did not really recognize that.

3 Likes

Place a local script into StarterPlayerScripts and paste in this script. I tested, it worked.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local descendants = workspace:GetDescendants()

for i, descendant in pairs(descendants) do
	if descendant:IsA("ClickDetector") then
		descendant.MouseHoverEnter:Connect(function()
			mouse.Icon = "rbxassetid://6632175405" --Your icon here
		end)
		descendant.MouseHoverLeave:Connect(function()
			mouse.Icon = "rbxasset://textures/Cursors/KeyboardMouse/ArrowFarCursor.png" 
		end)
	end
end

Basically you just getting everything in workspace, and checking if it is clickdetector. And using MouseHoverEnter, MouseHoverLeave to set the icon.

I don’t know if it will lagg with a lot of instances in workspace.

4 Likes

Nope, it’s still doing the same thing.

3 Likes

This would glitch out if a code creates a new click detector via Instance.new() after the game starts

3 Likes

I didn’t know. Then I don’t know how to fix that problem.

2 Likes

You need to repeat this proccess in a loop, but sadly it affects the performance badly.

3 Likes

But what if he connect the MouseHoverEnter and MouseHoverLeave events when creating the part, and when it’s needed just disconnect connection.

2 Likes

No that won’t get it fixed. You need to loop through workspace everytime to get updated with the descendants.

3 Likes

How wouldnt that work using Changed event is much more performance wise than checking in constant loop he even suggested to use Disconnect which is even better for the performance The way you are saying isnt wrong but its really bad for performance

3 Likes

idk about the disconnect and I forgot about Changed my bad.

2 Likes
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Cache = {} --Used to cache connections.

local function OnMouseHoverEnter()
	Mouse.Icon = "rbxassetid://0"
end

local function OnMouseHoverLeave()
	Mouse.Icon = "rbxassetid://0"
end

local function OnWorkspaceDescendantAdded(Descendant)
	if not (Descendant:IsA("ClickDetector")) then return end
	Cache[Descendant] = {
		Descendant.MouseHoverEnter:Connect(OnMouseHoverEnter),
		Descendant.MouseHoverLeave:Connect(OnMouseHoverLeave)
	}
end

local function OnWorkspaceDescendantRemoving(Descendant)
	if not (Descendant:IsA("ClickDetector")) then return end
	for _, Connection in ipairs(Cache[Descendant]) do
		Connection:Disconnect()
	end
	Cache[Descendant] = nil
end

Workspace.DescendantAdded:Connect(OnWorkspaceDescendantAdded)
Workspace.DescendantRemoving:Connect(OnWorkspaceDescendantRemoving)
for _, Descendant in ipairs(Workspace:GetDescendants()) do
	OnWorkspaceDescendantAdded(Descendant)
end
2 Likes