How to detect if the player is holding mouse button?

I’m trying to detect when the player is holding their left mouse button.

UserInputService.InputBegan:Connect(function(input)
   local inputType = input.UserInputType
   if inputType == Enum.UserInputType.MouseButton1 then
        hold = true
    end
end)    

UserInputService.InputEnded:Connect(function(input)
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = false
	end
end)

But when I click down the left mouse button, then right (while still holding left), and then release left, it somehow registers that as right and leads to infinite hodling. I managed to fix it adding this to InputEnded:

if inputType == Enum.UserInputType.MouseButton1 or inputType == Enum.UserInputType.MouseButton2 then

But now panning the camera with the right button cancels the holding. Is this a known bug or is there a better way to detect holding?

10 Likes

Would UIS:IsKeyDown() be a more elegant solution for this? Whoops, forgot that this is mouse, not keyboard. My bad.

Sounds like a weird bug if this is what happens, don’t have the time to test it at the moment though.

3 Likes

It may have something to do with the fact that you are using InputEnded twice.

I just tested the following code in studio. It does not have the issue you described:

--// Services
local UserInputService = game:GetService("UserInputService")

--// Objects
local Held = false

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		Held = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		Held = false
	end
end)

spawn(function()
	while wait(.1) do
		print(tostring(Held))
	end
end)
5 Likes

Sorry, my bad. I wrote that quickly for demo purposes, I do use InputBegan in the real code.

Edit: I’ll try again with your code just a sec.

Edit 2: Nope, the problem still persist. Could it be my mouse? It’s a Logitech MX Master which is afaik pretty common.

Weird that it’s still persisting, I’ve never encountered any problem like this before when dealing with mouse buttons being held.
Another method I’ve thought of to detect mouse buttons being held is UIS:GetMouseButtonsPressed() and just setting up a simple function to return whether or not a certain button is being held at the time it’s called. Try this:

local inputService = game:GetService("UserInputService")

local function isMouseButtonDown(inputType)
	for _,button in pairs(inputService:GetMouseButtonsPressed()) do
		if button.UserInputType == inputType then
			return true
		end
	end
	return false
end

-- Example:
held = isMouseButtonDown(Enum.UserInputType.MouseButton1)

If this still doesn’t work, I’d assume it’s something to do with your mouse. If it does work, it’d still be interesting to find out what the problem was with InputBegan and InputEnded which resulted in infinite holding.

5 Likes

Same issue :confused: keeps printing true even when I have released all mouse buttons after doing the sequence. No one else is seeing this? I guess it’s not an issue as long as it doesn’t affect my players.

game:GetService("UserInputService"):IsMouseButtonPressed(Enum.UserInputType.MouseButton1)

This pulls direct input from the mouse, ignoring UIs and other possible interruptions. After setting your ‘holding’ var to true, you can loop this until it’s false and then set ‘holding’ to false.

Same issue. If I press down left, then right, and release left it will keep registering left as being held down. It’s like I can sneak past the “no longer pressed” event if I have both buttons held down at the time.

have you tried the player mouse events?

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

local held = false
mouse.Button1Down:connect(function()
     held = true
end)

mouse.Button1Up:connect(function()
     held = false
end)

spawn(function()
     while true do
          wait(.1)
          print(held)
     end
end)
1 Like

I haven’t. But I believe mouse events doesn’t register when hovered over GUI which means if I were to release the button then it would lead to infinite holding too.

I don’t think you’re going to find a solution for this. You’ll need to report it as a bug. You’re on MacOS I assume? Does this happen only in Studio, or in-game as well?

6 Likes

It doesn’t happen in-game :slight_smile: guess it’s a non-issue then. Yeah MacOS …

4 Likes

This is indeed a bug. We’ll investigate.

16 Likes

Has this bug been fixed yet? I’m also experiencing this problem for my game too.

1 Like

You should make a bug report then

2 Likes