Detect when mouse Icon changes

Hey,
I’ve been trying to get whenever the mouse icon changes.

The function never starts, i tried by hovering a button to make it change but it doesn’t start anyway.

--Servizi
local UIS = game:GetService("UserInputService");
local Giocatori = game:GetService("Players"); 

--Variabili
local Giocatore = Giocatori.LocalPlayer; --player
local mouse = Giocatore:GetMouse(); -- mouse

function Muove()
	--this one works
end

function Cambia()
	print("ok") --never gets printed
end

mouse:GetAttributeChangedSignal("Icon"):Connect(Cambia)
mouse.Move:Connect(Muove)

Thank you all!

1 Like

It is impossible to do this directly, you need to check the mouse icon each time (loop) and compare it with the previous one, if they are different, then they have changed and call the function.

1 Like

oh that’s kinda annyoing.
Is there any other way?
Maybe by getting what type of UI the mouse is currently on?

I didn’t understand what this means, but you can’t do another way to check the mouse icon.

you can try this!

local mouse = game.Players.LocalPlayer:GetMouse()

function OnIconChanged()
print(“Mouse icon changed!”)
end

mouse.IconChanged:Connect(OnIconChanged)

A mouse does not have such a property.
Docs: Mouse | Roblox Creator Documentation

You could try to check the Mouse.Target every time Muove is ran, to see if the mouse is hovered over your button, and change the icon from there. If you have more than 1 button, consider using CollectionService to detect if a target has a Button tag on it. You can ask me about that if you’re not sure how to use it.

--Servizi
local UIS = game:GetService("UserInputService");
local Giocatori = game:GetService("Players"); 

--Variabili
local Giocatore = Giocatori.LocalPlayer; --player
local mouse = Giocatore:GetMouse(); -- mouse

local ButtonIcon -- Make this your button icon asset 
local DefaultIcon = 'rbxasset://SystemCursors/Arrow' -- Just the default roblox Icon

local Button -- Make this your physical button Part in the workspace

function Muove()
   if mouse.Target == Button and UIS.MouseIcon ~= ButtonIcon then
       UIS.MouseIcon = ButtonIcon
   elseif mouse.Target ~= Button and UIS.MouseIcon == ButtonIcon then
       UIS.MouseIcon = DefaultIcon
   end
end

-- Since the Mouse object is planned to get deprecated in the future, use the new UserInputService functions
UIS.InputChanged:Connect(function(Input)
  if Input.UserInputType == Enum.UserInputType.MouseMovement then
    Muove()
  end
end)
2 Likes

I think you can be more resourceful in your replies, seems like you’re only telling the person what they cannot do, and not providing much solutions for them. Perhaps, you can give them a code example to explain the solution you provided. This could help them solve their issues much better. :wink:

1 Like

I just realize my mistake thanks for the heads up!

1 Like

I don’t think this will work since it needs a UI button and not a BasePart.
I suggest using MouseEnter/MouseLeave for the button, that would be the best option.

button.MouseEnter:Connect(function()
     --when mouse enters
end)

button.MouseLeave:Connect(function()
    --when mouse leaves
end)

Docs: GuiObject | Roblox Creator Documentation

3 Likes

I’ve tried this method and it doesn’t work with UI sadly.
Thank you a lot anyway, i appreciate the help.

1 Like

Thank you a lot! it works :smiley:
I’m going to apply it to my script

2 Likes

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