Why wont the mouse.Changed event fire?

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

mouse.Changed:connect(function(x)
    print(".")
    if x == "Target" then
        print("NewTarget")
    end
end)

the mouse.Changed event wont fire and doesnt even print any “.”

does it have anything to do with me using a local script? help

What are you trying to do? If you’re trying to detect when the mouse is moved, .Changed isn’t the event you need. You’ll need .Move

Make sure you use the right event for what you need. Check this Mouse | Documentation - Roblox Creator Hub for any other events you may need.

No. The mouse logic should (and can only) be done from a localscript.

no i am not try to detect when the mouse has moved, im trying to detect specifically when the mouse.target has changed.

Please use Preformatted text to make your code more understandable for other Developers and use it in the future topics. Thank you.

There is nothing wrong with your code, are you running the localscript outside of where it can properly run? Try placing it in StarterPlayerScripts and see if anything changes.

i have the local script in starterpack

local LastTarget
Mouse.Move:Connect(function()
	if Mouse.Target then
		if not LastTarget then
			LastTarget = Mouse.Target
		else
			if LastTarget ~= Mouse.Target then
				LastTarget = Mouse.Target
			end
		end
	end
end)

This is what you need.

Mouse.Target is what your Mouse is currently over.
If LastTarget is not equal to what the current Mouse.Target is then it’ll set LastTarget to Mouse.Target, after that you can do the code you want to do.

1 Like

i tried placing the local script in StarterPlayerScripts and it still doesnt work

mouse.Changed doesn’t seem to have the desired effect in that it doesn’t fire when the mouse’s properties change (whether this is a bug or not, i don’t know).

However, you can get a similar effect using the mouse.Move

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

local target = nil

mouse.Move:Connect(function()
	local new_target = mouse.Target
	if new_target ~= target then
		print("New Target")
		target = new_target
	end
end)

p.s Use three ` to format code

The target will only change if the mouse moves, so this has the same effect as the code in your original post.

Perhaps the .Changed event doesnt fire when the mouse is moved? iF it did then they wouldnt need a .Moved event.

im not sure if having the programmer tag means you work for roblox, but if you do can you notify them about this bug?

I tried using the mouse.Move event, but for example if a new part were to appear in front of my mouse i would have to move it again in order for the event to fire, there are many ways for the mouse.target to change without moving your mouse.

The Programmer tag just means the user is in a group on the devforums called Programmer. The groups are used so that other users can easily find out what area a user specialises in.

You can put the code that I provided above in a RenderStepped or a while loop so that it’ll keep checking even when you don’t move your mouse.

local LastTarget
game:GetService("RunService").RenderStepped:Connect(function()
    if Mouse.Target then
		if not LastTarget then
			LastTarget = Mouse.Target
		else
			if LastTarget ~= Mouse.Target then
				LastTarget = Mouse.Target
                --run the code you wanna run here
			end
		end
	end
end

i guess i will for now, but it wont be as effecient

Of course it won’t be as efficient as only running the code when the mouse moves but that doesn’t make the code inefficient.

Remember to mark whichever reply you used as the solution.

last question, how can i notify roblox about this bug so they can fix it in the future

Create a detailed post in the #bulletin-board saying what the bug is and a way to reproduce the bug.

Then message @/Lead_Top_Contributors and request them to move it to #platform-feedback:engine-bugs

I’m not entirely sure that this is a bug, another thread in #development-discussion from over a year ago mentions that this is because mouse.Target is evaluated when you check the property. Considering this wasn’t reported as a bug then, it’s probably not a bug even if it’s not the best behavior.

the mouse.target part is irrelevant, im referring to the mouse.Changed event not firing

This isn’t a bug. The changed event only fires when one of the mouse’s underlying properties change. Because ‘Target’ isn’t one of those properties, the changed event won’t fire.

It will however, if one of these properties change:

image

1 Like

if you looked at my code earler i mentioned how it doesnt print a “.” under the mouse.Changed event