Best way to detect double input?

Im trying to detect clicking both left and right mouse at the same time, it works but left click/right click still happens before it how would i stop it?

local Services = {
	["UserInputService"] = game:FindService("UserInputService") or game:GetService("UserInputService"),
	["Players"] = game:FindService("Players") or game:GetService("Players"),
}

local player = Services.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Animations = {
	
}

local LastLeftClick = tick()
local LastRightClick = tick()

function changeCameraPrespective()
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		player.CameraMode = Enum.CameraMode.Classic
	end
	
end

Services.UserInputService.InputBegan:Connect(function(input,gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if input.KeyCode == Enum.KeyCode.LeftAlt then -- Change Camera Prespectives
		changeCameraPrespective()
	elseif input.KeyCode == Enum.KeyCode.LeftControl then -- Crouch
		print("started crouching")
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then -- Left Click
		LastLeftClick = tick()
		
		if (math.abs(LastRightClick - LastLeftClick)) < 0.1 then  -- Left AND RIGHT CLICK!
			print("Clcked both")
		else
			print("Left Clicked")
		end
		
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right Click
		LastRightClick = tick()
		
		if (math.abs(LastLeftClick - LastRightClick)) < 0.1 then -- Left AND RIGHT CLICK!
			print("Clcked both") 
		else
			print("Right Clicked")	
		end
		
	end
	
end)

Services.UserInputService.InputEnded:Connect(function(input,gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if input.KeyCode == Enum.KeyCode.LeftControl then
		print("stopped crouching")
		
	end
	
end)

image

I think its because the game has to process each input once at a time, so it processes the left clicked first then prints out the statement “left clicked” because the difference between LastRightClick and LastLeftClick is greater than 0.1, then it prints out “clcked both” when processing the right click

I dont think you can solve this with user input service, maybe either have some extra check in some other logic or maybe check for both inputBegan and inputEnded??? Instead it seems the solution (which roblox prefers) would be using ContextActionService as opposed to UserInputService

Wait 0.1 seconds and check if both have been clicked. If that’s the case, then return, else, process that singular left/right click. This can’t be done without delay as you can’t predict the future so you can’t see what input happens after. Alternatively, you can create events for button up and if one button is still down then don’t process it. If both come up within 0.1 seconds, then process it as double input, else, handle it like a normal input.

local Services = {
	["UserInputService"] = game:FindService("UserInputService") or game:GetService("UserInputService"),
	["Players"] = game:FindService("Players") or game:GetService("Players"),
}

local player = Services.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Animations = {
	
}

local LastLeftClick = tick()
local LastRightClick = tick()
local BothClickDebounce = false

function changeCameraPrespective()
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
	else
		player.CameraMode = Enum.CameraMode.Classic
	end
	
end

Services.UserInputService.InputBegan:Connect(function(input,gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if input.KeyCode == Enum.KeyCode.LeftAlt then -- Change Camera Prespectives
		changeCameraPrespective()
	elseif input.KeyCode == Enum.KeyCode.LeftControl then -- Crouch
		print("started crouching")
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then -- Left Click
		LastLeftClick = tick()
		
		task.wait(0.05)
		
		if (math.abs(LastRightClick - LastLeftClick)) < 0.05 and BothClickDebounce == false then  -- Left AND RIGHT CLICK!
			print("Clcked both")
			BothClickDebounce = true
			
			task.spawn(function()
				wait(0.05)
				BothClickDebounce = false
			end)
			
		else
			if BothClickDebounce == true then return end
			print("Left Clicked")
		end
	
		
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right Click
		LastRightClick = tick()
		
		task.wait(0.05)

		if (math.abs(LastLeftClick - LastRightClick)) < 0.05 and BothClickDebounce == false then  -- Left AND RIGHT CLICK!
			print("Clcked both")
			BothClickDebounce = true
			
			task.spawn(function()
				wait(0.05)
				BothClickDebounce = false
			end)
			
		else
			if BothClickDebounce == true then return end
			print("Right Clicked")
		end


	end
	
end)

Services.UserInputService.InputEnded:Connect(function(input,gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if input.KeyCode == Enum.KeyCode.LeftControl then
		print("stopped crouching")
		
	end
	
end)

figured it out here what i did if yall having the same issue

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