Why is this repeat loop not breaking efficiently?

Hello, I’m currently making a reaction time test minigame, however it is a bit different as they must click on the right word. The code works properly, however this section below is a bit inefficient. The repeat loop does not break once clicked, instead it waits the full 3 seconds until it goes to “Wrong Word!” Is there any way to make this break immediately? Thanks!

if not Correct then
		repeat	
			green.TextButton.MouseButton1Down:Connect(function()
			Wrong = true	
			end)		
				
			if wait(3) then
				break
			end
		until Wrong == true	
1 Like

I assume you want to do this?

local clickConnection = green.TextButton.MouseButton1Down:Connect(function() -- Connect MouseButton1Down
	Wrong = true
end)	

repeat task.wait() until Wrong -- Yield

clickConnection:Disconnect() -- Disconnect MouseButton1Down

I am not sure why you are using a condition with a delay of 3 seconds before breaking your loop. If you want to break the loop if it has been more than 3 seconds, just let me know, and I’ll show you how.

Hope I’ve helped a bit.

2 Likes

You could check if 3 seconds have passed or if the button was press in the ‘until’ section like this:

if not Correct then
    local t = tick()
    repeat	
        green.TextButton.MouseButton1Down:Connect(function()
		   Wrong = true	
	    end)		
        task.wait()
	until Wrong == true	or tick() - t >= 3
1 Like

Be careful; in your answer, you create an unnecessary number of connections. You need to connect MouseButton1Click outside the loop.

1 Like

This works for when the player clicks, but I want it to disappear after 3 seconds so it resets. Basically, the player needs to recognize that it’s the wrong word, and when they do, it keeps cycling through.

1 Like

So using tick is the better way

local startTick = tick()

local clickConnection = green.TextButton.MouseButton1Down:Connect(function() -- Connect MouseButton1Down
	Wrong = true
end)	

repeat task.wait() until Wrong or (tick() - startTick) > 3 -- Yield

clickConnection:Disconnect() -- Disconnect MouseButton1Down

You can also use os.time.

For some reason, now it takes a long while to show a word, and the only word that shows in the correct one? I think one issue is I’ve already used tick in a line before.

Here’s what I think went wrong:

if ready then
	green.TextLabel.Text = randomWord
	t = tick()
	green.Visible = true
	red.Visible = false
	if randomWord == "Click!" then
	Correct = true		
	end
		
	if not Correct then
		local startTick = tick()
--your code

There is no link between the two variables using tick. You named them with different names, they don’t interact with each other. I’m not entirely sure about the exact problem in this script. Can you provide more details about the issue? Perhaps use a little gif or video to demonstrate how it behaves during testing and explain what it’s supposed to look like?

This is what happens with the script I posted initially:

Oh, wait nevermind, the script works now. That’s weird, but thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.