Is There a Working Way to Get an Event for Mouse.Target?

I have a script that inserts a BillboardGui into the Mouse’s target if it meets a few requirements. Funny thing is, I haven’t really found a way to launch the code when the target changes. I’m desperate not to use a loop, and I’ve tried these:

image
image

1 Like

You can use UserInputService.InputChanged to detect when the player moves their mouse and then run the if statement.

1 Like

Pardon my ignorance, but how would you get a mouse target as a BasePart with it? I checked the Wiki and it only passes two arguments, both not an instance from workspace.

edit: wait my bad you meant use mouse.Target lol

2 Likes

This isn’t sufficient. The camera may change CFrame without InputChanged firing (e.g. the character gets moved or is already moving), or objects in the 3D world may change Size/CFrame/Shape. Either of these may cause the mouse’s “Target” to change.

A reliable way to do this is to not rely on legacy mouse API, but instead cast a ray every Render frame along the vector given by the relevant Camera API (casting through the pixel on the screen where the mouse currently is).

4 Likes

Simply use the mouse.Move event. This will fire when the mouse is moved not necessarily when the mouse target changes.

I haven’t tried it myself, but Instance's GetPropertyChangedSignal(propertyName) may allow you to get a signal for when Mouse.Target changes. This should fire when both the player’s mouse moves or the camera’s CFrame changes.

1 Like

I shortly realized that after I tested it. Currently, I just gave in and started using a loop for convenience.

Maybe if I find out how to remotely do that :sweat_smile: