I decided to make a thing where you can test how long it takes you to react when the red goes and green button appears, but:
Does break break only the inner loop in a nested loop? Because it seems to be breaking my outer loop too.
Is this code even good? Is there any way I can improve it, is there anything wrong with it?
Local script
local gui = script.Parent -- gui
local red = gui.Red -- red
local green = gui.Green -- green, what you click when it appears
local Time = gui.Time -- label showing your results
while true do
local randomTime = math.random(1,5)
wait(randomTime) -- random time to wait for until green appears
local t = tick() -- time when it appears
green.Visible = true
red.Visible = false
while wait() do
local clicked = false -- if it was clicked
green.MouseButton1Click:Connect(function()
red.Visible = true
green.Visible = false
local result = tick() - t -- 'result', tick - t is the time between start and when it was clicked
Time.Text = tostring(result)
clicked = true
end)
if clicked then break end -- if clicked then break the inner loop, instead it breaks the whole (or at least it stops working)
end
end
Basically the loop just stops after it finishes the first time, it never becomes green again, and I want to know if this code is terrible and I can improve it. (because I had no idea what I was doing, just typing random stuff and seeing if it would work, and I don’t know if there is a faster and easier way to do it,
example -
Do I really need a loop here to see if it was clicked?
First off, you shouldn’t be making a new MouseButton1Click connection every iteration of the loop. You can just use :Wait() with any signal to yield until the signal is fired. Also you should probably use MouseButton1Down instead of MouseButton1Click so you don’t have to actually raise the mouse button up for the click to register
local gui = script.Parent
local red = gui.Red
local green = gui.Green
local Time = gui.Time
while true do
local randomTime = math.random(1,5)
wait(randomTime)
local t = tick()
green.Visible = true
red.Visible = false
green.MouseButton1Down:Wait()
red.Visible = true
green.Visible = false
local result = tick() - t
Time.Text = tostring(result)
end
I’m trying to make it reset if you click red, (aka click too early), I made a coroutine (which I have little experience with) that checks if a value was changed, and then is supposed to reset it, I am thinking of using continue but I have absolutely no idea how. Is there even a way to do this?
What I did so far:
local gui = script.Parent
local red = gui.Red
local green = gui.Green
local Time = gui.Time
local resetValue = red.Reset
local reset = coroutine.Create(function()
while wait() do
resetValue:GetPropertyChangedSignal("Value"):Connect(function()
if resetValue.Value == true then
Time.Text = "Try again!"
-- Is there a way to somehow use continue on the loop below?
resetValue.Value = false
end
end)
end
end)
coroutine.resume(reset)
while true do
local randomTime = math.random(1,5)
wait(randomTime)
local t = tick()
green.Visible = true
red.Visible = false
green.MouseButton1Down:Wait()
red.Visible = true
green.Visible = false
local result = tick() - t
Time.Text = tostring(result)
end