Mouse Jumping Issue and Premature InputChanged Events

When using the provided Lua script for development purposes in Roblox, a problem arises. After unlocking the mouse, it consistently jumps to its previous position before locking whenever any movement occurs. Additionally, there is an issue where InputChanged events with MouseMovement are triggered prematurely, even when the mouse hasn’t moved at all. These behaviors disrupt user experience and visual consistency

Source Code
local UserInputService = game:GetService("UserInputService")

local function lockMouse()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end

local function unlockMouse()
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end

UserInputService.InputBegan:Connect(function(input: InputObject, _: boolean)
	print("InputBegan", input, input.UserInputType, input.UserInputState)
	lockMouse()
end)

UserInputService.InputChanged:Connect(function(input: InputObject, _: boolean)
	print("InputChanged", input, input.UserInputType, input.UserInputState)
end)

UserInputService.InputEnded:Connect(function(input: InputObject, _: boolean)
	print("InputEnded", input, input.UserInputType, input.UserInputState)
	unlockMouse()
end)

Steps to Reproduce:

  1. Run the provided Lua script in a Roblox game
  2. Lock the mouse using any trigger (e.g., pressing a designated key)
  3. Move the mouse to a different position
  4. Unlock the mouse using any trigger (e.g., releasing the key)
  5. Observe the mouse cursor’s behavior

Expected behavior

  1. After unlocking the mouse, the cursor should remain at its current position without any abrupt jumps
  2. InputChanged events should only be triggered when there is actual mouse movement

Actual Behavior:

After unlocking the mouse, the cursor jumps to its previous position before locking whenever any movement occurs. This makes it difficult for users to accurately control the cursor and disrupts gameplay or interactions relying on precise mouse movements

Additional Notes:

Attempting to show the mouse after detecting MouseMovement in InputChanged results in premature firing of InputChanged events, even when the mouse hasn’t moved at all

Impact:

The issue significantly affects user experience and visual consistency

Environment:

  • Operating System: Windows 10
  • Roblox Studio Version: Version 0.622.0.6220470 (64bit)
  • Device: Alienware Area-51 m

Workaround:

Currently, there is no known workaround for this issue without experiencing the cursor jump behavior

1 Like