How can I detect when the player stops moving their mouse?

Hello, I want to detect when the mouse is not being moved. Now I have tried mouse.Idle and mouse.Moved but neither of these are doing what I want. e.g when I have this code;

mouse.Idle:Connect(function()
	print("1")
end)

It will print every frame regardless of me touching my mouse why is this?

local mouse = game.Players.LocalPlayer:GetMouse()
local events = {"Button1Down","Button1Up","Button2Down","Button2Up","Idle","Move","WheelBackward","WheelForward","KeyDown","KeyUp"}
local currentEvent


for _,event in pairs(events) do
	mouse[event]:Connect(function ()
		currentEvent = event
	end)
end

Now you can check if the player mouse is idle using:

if currentEvent == "Idle" then
     print(the player mouse is Idle)
end
1 Like

But wouldnt this be the same as mouse.Idle:Connect(function()? The problem is idle isnt working for me if I spam move my mouse it still prints.

Maybe try waiting 3 seconds so it has to be idle for 3 seconds for the script to work

Still didnt work. I dont know if I’m using mouse.Idle wrong but this is my code;

mouse.Idle:Connect(function()
	wait(3)
	print("1")
end)

You could try detecting the mouse position in the mouse.Idle function and see if its different from the last position.

local lastpos = mouse.Hit
mouse.Idle:Connect(function()
if mouse.Hit == lastpos then
print("1")
end
end)
1 Like

I forgot to change the variable

local lastpos = mouse.Hit
mouse.Idle:Connect(function()
if mouse.Hit == lastpos then
print("1")
lastpos = mouse.Hit
end
end)
3 Likes

Umm, why was this classified as the solution? I copied it out and it doesn’t work, cus even logically the mouse has to be in the same very position since the spawn of the character and so it would never reference if it was standing still relative to the run time of the game

As a matter of fact, that post was indeed correct, you can find the reference
here (Mouse | Roblox Creator Documentation).

it should be

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

local lastpos = 0
mouse.Idle:Connect(function()
	if mouse.Hit == lastpos then
		print("Mouse isn't moving")
	end
	lastpos = mouse.Hit
end)
5 Likes