Fixing Reaction Time Test

The reaction time test works, there is just one problem: the wait time. Is there anyway to avoid the problem of people spam clicking?

Script:

local button = script.Parent
local buttonbackground = button.ButtonBackground
local waiting = false
local waitingforclick = false
local reactiontimestart

button.MouseButton1Down:Connect(function()
	if waiting == false and waitingforclick == false then
		button.Text = "WAIT FOR GREEN"
		buttonbackground.ImageColor3 = Color3.fromRGB(131, 0, 0)
		waiting = true
		wait(math.random(3200, 5200)/1000)
		if waiting == true then
			waiting = false
			waitingforclick = true
			button.Text = "CLICK!!!"
			buttonbackground.ImageColor3 = Color3.fromRGB(85, 255, 127)
			reactiontimestart = tick()
		end
	elseif waiting == false and waitingforclick == true then
		local reactiontime = tostring(tick() - reactiontimestart)
		local temp = string.split(reactiontime, ".")
		reactiontime = ""
		if tonumber(temp[1]) > 0 then
			local shorten = string.split(temp[2], "")
			for i = 1, 3 do
				reactiontime = reactiontime..shorten[i]
			end
			reactiontime = temp[1]..reactiontime
			print(reactiontime)
		else
			local shorten = string.split(temp[2], "")
			if tonumber(shorten[1]) == 0 then
				for i = 2, 3 do
					reactiontime = reactiontime..shorten[i]
				end
			else
				for i = 1, 3 do
					reactiontime = reactiontime..shorten[i]
				end
			end
		end
		button.Text = "Reaction Time: "..reactiontime.."ms"
		buttonbackground.ImageColor3 = Color3.fromRGB(170, 255, 255)
		waitingforclick = false
	elseif waiting == true and waitingforclick == false then
		button.Text = "TOO EARLY, \n CLICK TO START"
		buttonbackground.ImageColor3 = Color3.fromRGB(170, 255, 255)
		waiting = false
	end
end)

VIDEO OF PROBLEM:

1 Like

Add a debounce at the top of the script and a wait when they click too early.

local debounce = false

button.MouseButton1Down:Connect(function()
    if debounce then return end
    debounce = true

    if clickedTooEarly then
        -- Message about clicking too early
        task.wait(1)
    end

    debounce = false
end)

The Human Benchmark Reaction Test actually reacts the same way as your script currently. Maybe it’s better that way?

1 Like

I just tried this, and the clicked too early if statement isn’t even firing even though it meets the conditions :confused:

local button = script.Parent
local buttonbackground = button.ButtonBackground
local waiting = false
local waitingforclick = false
local debounce = false
local reactiontimestart

button.MouseButton1Down:Connect(function()
	if debounce then return end
	debounce = true
	
	if waiting == false and waitingforclick == false then
		button.Text = "WAIT FOR GREEN"
		buttonbackground.ImageColor3 = Color3.fromRGB(131, 0, 0)
		waiting = true
		wait(math.random(3200, 5200)/1000)
		if waiting == true then
			waiting = false
			waitingforclick = true
			button.Text = "CLICK!!!"
			buttonbackground.ImageColor3 = Color3.fromRGB(85, 255, 127)
			reactiontimestart = tick()
		end
	elseif waiting == false and waitingforclick == true then
		local reactiontime = tostring(tick() - reactiontimestart)
		local temp = string.split(reactiontime, ".")
		reactiontime = ""
		if tonumber(temp[1]) > 0 then
			local shorten = string.split(temp[2], "")
			for i = 1, 3 do
				reactiontime = reactiontime..shorten[i]
			end
			reactiontime = temp[1]..reactiontime
			print(reactiontime)
		else
			local shorten = string.split(temp[2], "")
			if tonumber(shorten[1]) == 0 then
				for i = 2, 3 do
					reactiontime = reactiontime..shorten[i]
				end
			else
				for i = 1, 3 do
					reactiontime = reactiontime..shorten[i]
				end
			end
		end
		button.Text = "Reaction Time: "..reactiontime.."ms"
		buttonbackground.ImageColor3 = Color3.fromRGB(170, 255, 255)
		waitingforclick = false
	elseif waiting == true and waitingforclick == false then
		button.Text = "TOO EARLY, \n CLICK TO START"
		buttonbackground.ImageColor3 = Color3.fromRGB(170, 255, 255)
		waiting = false
		task.wait(0.5)
	end
	
	debounce = false
end)

I just realized that the problem comes with the wait being in the first condition, since the wait is on, the debounce can’t go back to being false unless completing the wait for the click opportunity, therefore it’ll never show the clicked to early message