How can I detect a double M1 click?

Hi DevForum,

I tried making a double click system using the tick() function, however I don’t know what I am doing wrong.

I don’t really know much about os.clock() and stuff, and am pretty new to using the tick() function.

Here is my code:

local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = script.Parent


local lastM1Time = 0


UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then				
				if tick() - lastM1Time < 0.2 then
					
					lastM1Time = tick()
					
				end
			
	end
end)
3 Likes

I think what went wrong was that the lastM1Time could never be changed if it’s been more than 0.2 seconds. Basically, convert the UIS event function to this, and you should be good:

UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then				
		if tick() - lastM1Time < 0.2 then
			
			-- Insert other code intended to run when double-clicking.
			
		end
		
		lastM1Time = tick() -- Reset variable to tick() value.
	end
end)
3 Likes

Thanks, I forgot to reset the lastM1Time, a small mistake, I appreciate the support!

1 Like