Going to start of a loop

Yeah I had thought it wasn’t the best idea, I primarily wanted to point it out as a possible solution. :+1:

I did find this other DevForum post asking the same question you’re asking that received a solution, it may have a useful answer for you. You can find it here.

1 Like

Right, so basically it’s a human reaction time test.

The first time you start, you’re given a UI with the appropriate text telling you how it works. Once you’ve clicked the button, the actual test starts (hence the button.MouseButton1Click:Wait() at the start of the loop). After clicking, the visuals changes, telling you to click again once the screen changes to a different colour. If you click before the visual changes, the function linked with the variable “connection” would run. When it runs, it tells you that you’ve clicked too soon, then you click again to restart this test (hence restarting the loop). If you click after the visual changes, it tells you the time it took for you to click in microseconds in visuals, displaying a total amount of tries and the average reaction time. This is why you can continuously repeat the test, to improve (or worsen) your average.

EDIT: Realised that contrary to a prior post, there are no clicks thereafter if you exclude the ones that happen after the loop repeats. Might go with your idea of keeping it connected then, I’ll see how it works out when I’m able to access studio.

Just a an example to demonstrate what you could ish do

local active = false
local timestamp = tick()

something.MouseButton1Click:Connect(function()
	if active then
		active = false
		local reaction_time = tick() - timestamp
		-- this is how fast they clicked

	else
		-- not active / premature
	end
end)

-- other logic handling the game based on events for example
timestamp = tick()
active = true
1 Like

Okay, so I completed it, incorporating @konlon15’s idea of wrapping it in another while true loop and using a break to force it to repeat (also mentioned in the link given by @CleverSource) , as well as @gullet’s idea of not disconnecting it. After finishing it, I realised that I forgot to use time stamps, but it works so. :man_shrugging:

For those interested, the code is more like this now:

local initialClick
local triggerable
local triggered

button.MouseButton1Click:connect(function()
    if triggerable and not initialClick then
        triggered = true 
        -- visuals  
        return 
    end
end)

while true do 
while true do
    initialClick = true
    triggered = false
    triggerable = false
    button.MouseButton1Click:Wait()
    initialClick  = false
    -- visuals
    local period = x -- where x is the value of a random int.
    local currentWait = 0


    while currentWait < period do
        currentWait = currentWait + wait(.1) -- For accuracy purposes,     print(wait(.1)) shows that wait fluctuates a bit. Using .1 since the end value is an integer, so no issues there.
    triggerable = true -- click function runs long after the event fires, put after .1 delay so it works
        if triggered then break end
        end
    if triggered then break end
    triggerable = false
    -- other code

end
end

It’s not the prettiest, but I’m satisfied! Thanks to everyone who contributed with their ideas. :slight_smile:

2 Likes