Best way to detect if someones clicked twice within a certain time frame?

I’m looking to check if someone has clicked twice within say two seconds, and if so… do a certain action; if not… then it does something different.

local user_input = game:GetService " UserInputService"
local clicks = 0

user_input.InputBegan:Connect(function(already_processed, obj)
  if already_processed then return end
  if obj.UserInputType ~= Enum.UserInputType.MouseButton1 then return end

  clicks += 1
  if clicks > 2 then
      --do your action
  end

  task.wait(2)
  clicks -= 1
end)

you can also do this with ContextActionService.

edit: oh you want to do something else when it has been longer than two seconds?
oh ok:

local user_input = game:GetService "UserInputService"
local clicks = 0
local last_time = nil

user_input.InputBegan:Connect(function(already_processed: boolean, obj: InputObject)
   if already_processed or obj.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
   clicks += 1

   if not last_time then last_time = tick() return end
   if tick() - last_time <= 2 and clicks >= 2 then
        clicks = 0
        --do your action
   else
       --do your other action
   end

   last_time = tick()
end)
1 Like

Here’s a simple detection:

local click_counter = 0

script.Parent:WaitForChild("TextButton").Activated:Connect(function()
	print("Clicked")
	click_counter += 1
	
	print("Starting timer")
	for i = 2, 0, -1 do -- start from 0 with a timer of 2 seconds, and every 1 second it reduces the timer by 1
		if click_counter == 2 then
			print("The click goal was reached:", click_counter)
		end
		task.wait(1) -- every 1 second
	end
	print("Timer ended")
end)
1 Like

so, are you able to do this on the server side as well…?

Yes, just do it on each client in a localscript. And as action trigger a remote to tell the server:
On the client:

local user_input = game:GetService " UserInputService"
local remote = --your remote
local clicks = 0

user_input.InputBegan:Connect(function(already_processed, obj)
  if already_processed then return end
  if obj.UserInputType ~= Enum.UserInputType.MouseButton1 then return end

  clicks += 1
  if clicks > 2 then
      remote:FireServer()--tell server
  end

  task.wait(2)
  clicks -= 1
end)

On the server:

local remote = --your remote
remote.OnServerEvent(plrWhoTriggered: Player)
--your action
end)
1 Like