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
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.
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.
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.
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’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.
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: