How would I measure the mobile equivalent of Clicks per second?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I would like to effectively measure how many times per second a user is tapping on an ImageButton on the screen.

  2. What is the issue? Include screenshots / videos if possible!
    I already have code in place that measures clicks per second, which works correctly for PC players. But when I test the game, clicking normally from a phone makes the clicks per second measurement freak out. The label showing what the measurement is at any given time, shows that the clicks per second skyrockets into more than 10,000 CPS, which can’t be right!

  3. What solutions have you tried so far?
    Below is my existing code to track clicks per second.

local CPS_TEMPLATE = "(+AMOUNT clicks/sec)"

script.Parent.Cookie.MouseButton1Click:Connect(function()
	if lstats ~= nil then lstats.Cookies.Value += 1 end --lstats is my variable I set elsewhere in the script to represent leaderstats for the local player.
	local currentTime = tick() -- get the current time
	local timeDelta = currentTime - lastClickTime -- calculate the time difference
	local clicksPerSecond = math.round(1/timeDelta) -- calculate clicks per second
	lastClickTime = currentTime -- update the last click time
    UpdateCPS(clicksPerSecond)
end)

script.Parent.Cookie.TouchTap:Connect(function()
	if lstats ~= nil then lstats.Cookies.Value += 1 end
	local currentTime = tick() -- get the current time
	local timeDelta = currentTime - lastClickTime -- calculate the time difference
	local clicksPerSecond = math.round(1/timeDelta) -- calculate clicks per second
	lastClickTime = currentTime -- update the last click time
    UpdateCPS(clicksPerSecond)
end)

function UpdateCPS(amount: number)
	local clicksAmount = amount - PreviousClicksAmount
	PreviousClicksAmount = amount
	if clicksAmount <= 0 then return end
	ClicksDuringSecond += clicksAmount
	script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
	script.Parent.lblCPS.Visible = true
	task.delay(1, function()
		ClicksDuringSecond -= clicksAmount
		script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
		script.Parent.lblCPS.Visible = ClicksDuringSecond > 0
	end)
end

Can anyone tell me why this doesn’t work properly on mobile, but it does on PC? I’m quite confused. Any help would be greatly appreciated, thanks!

Edit: I added the other function I was using.

Use activated not mousebutton1, as there is no mouse button on mobile

I also showed that I was using a second event; TouchTap. I’ve been using that event to register “clicks” on mobile. The code underneath the connection for that event is identical to the code I’m using to track clicks on PC. It shouldn’t just be the MouseButton1Click event that’s interfering with mobile users.

I haven’t messed with something like this, but have you tried to use a debounce to rate limit the clicks? Also, since the code in the two events are the same, you can put them in a separate function and just call them from the event handler.

local CPS_TEMPLATE = "(+AMOUNT clicks/sec)"
local debounce = true

function UpdateCPS(amount: number)
	local clicksAmount = amount - PreviousClicksAmount
	PreviousClicksAmount = amount
	if clicksAmount <= 0 then return end
	ClicksDuringSecond += clicksAmount
	script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
	script.Parent.lblCPS.Visible = true
	task.delay(1, function()
		ClicksDuringSecond -= clicksAmount
		script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
		script.Parent.lblCPS.Visible = ClicksDuringSecond > 0
	end)
end

local function handleEvent()
	if debounce == true then
		debounce = false
		if lstats ~= nil then lstats.Cookies.Value += 1 end --lstats is my variable I set elsewhere in the script to represent leaderstats for the local player.
		local currentTime = tick() -- get the current time
		local timeDelta = currentTime - lastClickTime -- calculate the time difference
		local clicksPerSecond = math.round(1/timeDelta) -- calculate clicks per second
		lastClickTime = currentTime -- update the last click time
		UpdateCPS(clicksPerSecond)
		delay(0.1, function()
			debounce = true
		end)
	end
end

script.Parent.Cookie.MouseButton1Click:Connect(function()
	handleEvent()
end)

script.Parent.Cookie.TouchTap:Connect(function()
	handleEvent()
end)

Something is causing a click to register on mobile much faster than humanly possible. Even me, I can manage about 5-6 per second max. So the debounce will rate limit the number of times it will take it to 10 clicks per second. Then you need to find out why you are getting so many at once.