Is there a way to detect when a player clicks their mouse two times or more?


I want to be able to detect how many times a player clicks, while also resetting the value that keeps track of how many times they clicked within a certain set time.

Is it possible to detect how many times a player clicks, while also giving that value a timer before it resets back to 0?


3 Likes
4 Likes

Yes. You could have an initiate variable counter (0) and add +1 for each time the player is clicking his mouse.

And you could also use time.os , tick or any other variable for the timer.

Forummer has given you a great example.

1 Like

To achieve this, you can have a variable that keeps track of clicks and another that keeps track of when the clicks should reset. I’m going to make use of no loops to reset the clicks as that is a waste of resources, instead I will be using math to determine when the clicks should reset:

--Services
local UIS = game:GetService("UserInputService")


--Variables
local Clicks = 0
local ResetTime = 5 --The time interval for resetting the Clicks

--local startTime = os.clock()
local currentTime = os.clock() --Variable to store when the clicks should be resetted


--Event
UIS.InputBegan:Connect(function(Input:InputObject, GameProcessedEvent)--Event to detect when the user interacts with game
	if GameProcessedEvent then return end --Comment out this line if you don't want to ignore Game processed clicks (ex: clicking on a textbutton, clicking on a textbox, etc) 
	
	
	if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then --It was a click!
		local Time = os.clock()
		if Time - currentTime >= ResetTime then 
			Clicks = 0
			currentTime = Time
		end
		
		Clicks += 1
		
	end
end)
2 Likes

First you need is to define tick(). After that you’re checking if time passed is equel or smaller than provided. If it’s not then you’re updating the last tick.

local prev = tick()
UIS.InputBegan:Connect(function() -- or UIS.TouchTap for mobile
	if tick() - prev <= .5 then -- or any time
		-- code
	else
		prev = tick()
	end
end)

EDIT: I saw that this post is already solved but that is the simple way.

1 Like

This is exactly what I was looking for, a way that would allow me to detect how many times a user clicked, while also being able to reset it after a certain set time. Thank you so much!