How do I make a click detector detect a double click?

Hello!
I’m trying to create minesweeper on roblox, and I aim to make the player flag a square when it’s double clicked, in order to make the game mobile compatible (instead of just right clicking the square).

I’ve searched around for a while searching if there’s a way to detect a double click with a click detector, but all of the forum posts I’ve found use the user input service, and when I try to follow the solutions from those threads, the game detects a single click after every double click.

This is the code I’ve currently got:

local timeGap = 0.5
local clicks = 0
local threshHold = 2

function onClick()
	clicks += 1
	
	if clicks%threshHold == 0 then
		return true -- Double Click
	end
	
	wait(timeGap)
	
	clicks -= 1
end

The issue with this is that after a double click, the number of clicks is set to 1, so the next time I click it goes back to 2 again, and detects it as a double click.
However, if I set the number of clicks to 0 at the end of the script, then the game detects a single click after I’ve double clicked, which also isn’t ideal.

Is there any way for me to work around this?
Thanks!

Instead of using a boolean and waiting, you might want to just store the last click time:

local timeGap = 0.5 -- Might want to make this a little lower.
local lastClickTime = tick()

function onClick()
    local currentTime = tick()
    -- If the time between the current click and the last click is less than the gap time.
    if currentTime - lastClickTime < timeGap then
        lastClickTime = 0
        return true
    else
        lastClickTime = currentTime
    end
end

To support larger click counts that 2:

local timeGap = 0.5 -- Might want to make this a little lower.
local lastClickTime = 0
local threshHold = 2
local clicks = 0

function onClick()
	local currentTime = tick()

	-- If the time between the current click and the last click is less than the gap time.
	if currentTime - lastClickTime < timeGap then
		clicks += 1
	else
		clicks = 1 -- If the click isn't chained then the count returns to zero
	end

	lastClickTime = currentTime

	if clicks > threshHold then
		clicks = 0 -- Optional: Reset clicks. If you don't, chained clicks larger than the threshold don't count.
		return true
	end
end

you can use this:

local ClickDetector = script.Parent.ClickDetector

local LastClick = 0
ClickDetector.MouseClick:Connect(function()
	if LastClick and tick()-LastClick < .5 then
		print("Double Click!")
	end
	LastClick = tick()
end)

I’ve tried to use both of these and while they do accurately detect a double click, it still sends an output on the first click (if that makes any sense)
Is there a solution to this?

1 Like

You can use a ClickDetector’s MouseDoubleClick event, which is fired when the player double clicks the ClickDetector.
Example:
local clickDetector = script.Parent

function onDoubleClick()
– Double click
end

clickDetector.MouseDoubleClick:Connect(onDoubleClick)

I’m fairly sure that’s not a thing

1 Like