Right click detection

How should I prevent the mouse.Button2Down event from firing if the supposed right clicking action is just the player trying to turn its camera?

If there are any other methods of detecting right clicking ignoring camera rotation, let me know.

local Players = game:GetService("Players")

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

mouse.Button2Down:Connect(function()
	--
end)
2 Likes

Don’t use the mouse object for your input - Roblox themselves said the only reason it is not deprecated is because of its use in other games already.Detecting on UI buttons, you can use the MouseButton2Click, MouseButton2Down, MouseButton2Up events.Otherwise, use the UserInputService or ContextActionService with the Enum.UserInputType.MouseButton2 Enum.

the gameProcessedEvent might work for your issue (UserInputService). If not, you could always wait a little time and use UserInputService:IsMouseButtonPressed() to check. Alternatively, you could listen for InputBegan and InputEnded events and compare the time between them, also checking for the MouseButton2 input type.

local function onInput(input:InputObject, processed:boolean)
    if processed then return nil end
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        task.wait(1)
        if uis:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
            print("Camera was moved")
        end
    end
end
2 Likes

Use contextactionservice. You can use it so it can only active when it needs to

That wouldn’t work. ContextActionService is used for mobile buttons and KeyCodes. Right click isn’t an option from both.

I meant using BindAction.

ContextActionService:BindAction(...)

I think you can do this with mouse events

This is incorrect. ContextActionService uses a Boolean parameter to determine whether a mobile button should be created; you must then pass any number of parameters to bind to the action. These can be Enum.KeyCodes or Enum.UserInputTypes.

1 Like

If I’m correct, there exists a value for the MouseButton2Down

So, yea, I guess it would work, maybe?

My mistake on that. Forgot about Enum.UserInputType being a valid argument.

1 Like

Ok. Thanks for the advices, but I find using ContextActionService is better for the game. I’ll just remap the keybind to Enum.KeyCode.E instead of Enum.UserInputType.MouseButton2, since I still haven’t found other solutions online about ignoring it if it’s only used for camera rotation. I’ll keep this post open until someone replies with a better solution. Thank you all again.

2 Likes

Actually, this seems like a good solution. I’ll think about implementing this.

Update: Using ContextActionService and UserInputService works best for the game. Saved me some time from making a custom-made ui just for mobile. Both Enum.KeyCode.E and Enum.UserInputType.MouseButton2 have been implemented, it’s just that the function for Enum.UserInputType.MouseButton2 only runs if more than 0.2 seconds have passed. Thank you all for the feedback again!

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	local t = tick()
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		UserInputService.InputEnded:Wait()
		
		if tick() - t > 0.2 then return end

		print("Right clicked!")
	end
end)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.