Mouse position querying in UserInputService without event listeners

It’d be great if we could query the position of the mouse (such as in Mouse.X & Mouse.Y) from UserInputService without having to listen to MouseMovement InputChanged events. Prevents inconsistency in having to use the Mouse object alongside all-UserInputService-based code.

2 Likes

I personally can’t see the issue with listening to MouseInput. The idea of the service is that it is driven by input and therefore you should wait for input and track it yourself.

Mouse position is input. Following your logic we should deprecate the GamepadEnabled property and have to track that ourselves by listening to GamepadConnected/Disconnected, which is silly. Querying input-related properties of UserInputService is completely reasonable.

  • LocalScript needs to know the mouse position as soon as it’s run but doesn’t because the user hasn’t moved their mouse since the script started
  • Unnecessary logical (not resource) overhead: have to hook into InputChanged and process every input change, even if it isn’t mouse movement, as opposed to potentially querying just the mouse position every now and then
  • Anytime you need to repeatedly remake something from the ROBLOX API on your own is a big red flag that the API is flawed and has room for improvement. The API is a tool for us to use – not something we should need to remake entirely or parts of when we want to use it
2 Likes

Echo is above all correct because for me it’s become an issue when I have a game where you move your mouse to update a tool tip. Now you are in first person. If you just move with with keys mouse move can’t catch this. I am now updating mouse move and input changed to the same function since I don’t have full control of the mouse components from just UIS. User Input Service should replace GetMouse by all means.

You can leave out MouseMove because InputChanged picks up mouse movement as well. In all honesty you should probably be using RenderStepped or Heartbeat to update tooltips since unanchored objects (namely players) can move around in the world entirely separate from your own input and change what your mouse is hovering over.

I tried using it in input changed and it became glitchy and unresponsive to mouse movement, but I will tried render stepped, thanks for mentioning that.

If you want, I have a module that allows you to do this:

https://github.com/Sleitnick/TeamCrazyGameFramework/blob/master/src/game/StarterPlayer/StarterPlayerScripts/Main.Controls.Mouse.module.lua

I modeled it after the PlayerMouse object, except it only uses the Camera and UserInputService. It throws the mouse position into the X and Y properties of the object when the mouse is moved. Example:

local mouse = require(foo.bar.Mouse)
while (true) do
   print(mouse.X, mouse.Y)
   wait(1)
end
2 Likes