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?
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)
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.
Same issue 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.
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.
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)
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?