How would I go about creating a click-per-second detector script?

I’ve spent about a few hours on this and still can’t get the idea on how to get it to work. And it seems like a fairly easy question to answer until you get to a problematic part.

I’m trying to make a script that detects how many times a player clicks per second on a gui button and then display it to them. For example if they click 6 times per second it shows they clicked 6 times and if they clicked 4 times in a second then it shows them, but here’s where the problem starts. If they click 10 times but a second had already passed when they clicked 8 times. How would I make it so that it displays they clicked 8 times instead of 10 times?

So in summary how do I make it so that players see how many times they clicked during the passed second?

Here’s what I came up with

local ClickerInput = script.Parent.ClickerInput
local clicked = 0

local function start()
	local clock = os.clock()
	while true do
		print(clicked)
		if os.clock() - clock >= 1 then
			clock = os.clock()
			print("A second has passed!")
			clicked = 0
		end
		wait(0.1)
	end
end

ClickerInput.inputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		clicked = clicked + 1
	end
end)

local wrap = coroutine.wrap(start)
wrap()
--[[
  07:51:49.304  1  -  Client - Sample:16
  07:51:49.420  2  -  Client - Sample:16
  07:51:49.421  A second has passed!  -  Client - Sample:19
  07:51:49.537   ▶ 1 (x2)  -  Client - Sample:16
  07:51:49.757  2  -  Client - Sample:16
  07:51:49.871  3  -  Client - Sample:16
  07:51:49.986  4  -  Client - Sample:16
  07:51:50.087   ▶ 5 (x2)  -  Client - Sample:16
  07:51:50.303  6  -  Client - Sample:16
  07:51:50.404  7  -  Client - Sample:16
  07:51:50.505  8  -  Client - Sample:16
  07:51:50.505  A second has passed!  -  Client - Sample:19
  07:51:50.619  0  -  Client - Sample:16
]]

You can make a GUI or something , and before you reset the variable Clicked , you can set the textlable of the GUI as the Clickec variable.

he also can do a gui that resets every second with the clicks that happened in that second

Hello!

This is an example of a quick clicking speed test. The code is similar to yours and GUIs are organized this way:

image

Clicks label and ClickerInput text button don’t have any properties changed while game is running, as opposed to DisplayClicks, which displays number of clicks in a second, and DebounceGui, which only shows up for a short time as a cooldown notification.

image

local clicks = 0
local validClicks = true
local debounce = false

local debounceGui = script.Parent.Parent.DebounceGui
local displayClicks = script.Parent.Parent.DisplayClicks

local function ResetGuis()
	validClicks = true
	debounce = true
	displayClicks.Text = clicks .. " cps!"
	debounceGui.Text = "Debounce"; wait(2)
	debounceGui.Text = ""
	clicks = 0
	debounce = false
end

local function StartTimer()
	local clockStart = os.clock()
	repeat
		game:GetService("RunService").Heartbeat:Wait()
	until os.clock() - clockStart >= 1
	validClicks = false
	ResetGuis()
end

script.Parent.MouseButton1Down:Connect(function()
	if (not debounce) then
		if (clicks == 0) then
			local Wrap = coroutine.wrap(StartTimer); Wrap()
			displayClicks.Text = "0"
		end
		if (validClicks) then
			clicks += 1
			displayClicks.Text = tostring(clicks)
		end
	end
end)

EDIT

@Varsitelle I was working in studio when I found saved click speed test file that I’ve created yesterday. “Why on Earth didn’t I simply publish and Uncopylock it?” Anyways, here you go:

https://www.roblox.com/games/6385038137/Click-speed-test

3 Likes

This looks similar to what I was looking for. I can’t try it right now but i’ll definitely try it out later and see if it works.