I'm creating a code that will check your reaction speed. If you click the button a for loop will stop and tell your reaction speed, but when I click it and no matter how much time I wait, it still just displays 0. Is there any way to fix this?
The code is in a LocalScript in the TextButton.
Code:
01 | wait( 2 ) |
02 | print ( "ready to go" ) |
03 | local playergui = game.Players.LocalPlayer.PlayerGui |
04 | local TextButton = playergui.ScreenGui.TextButton |
05 | local randomtime = math.random( 2 , 4 ) |
06 |
07 | wait(randomtime) |
08 |
09 | TextButton.BackgroundColor 3 = Color 3. new( 0 , 225 , 0 ) |
10 | TextButton.Text = "Click!" |
11 |
12 | TextButton.Activated:Connect( function (activated) |
13 | for i = 0 , math.huge , 1 do |
14 | wait() |
15 | if activated then |
16 | TextButton.Text = (i) |
17 | break |
18 | end |
19 | end |
20 | end ) |
You should start your loop outside of function, because your loop starts only after a click. Better yet, do not use loop and use tick()
.
01 | wait( 2 ) |
02 | print ( "ready to go" ) |
03 | local playergui = game.Players.LocalPlayer.PlayerGui |
04 | local TextButton = playergui.ScreenGui.TextButton |
05 | local randomtime = math.random( 2 , 4 ) |
06 |
07 | wait(randomtime) |
08 |
09 | TextButton.BackgroundColor 3 = Color 3. new( 0 , 225 , 0 ) |
10 | TextButton.Text = "Click!" |
11 |
12 | --start loop here |
13 | local i |
14 | local clicked = false |
15 | for i = 0 , math.huge , 1 do |
However it is best to use tick()
for this task. Google tick()
for more info, but the short story is that you can get an accurate time difference thanks to it.
01 | wait( 2 ) |
02 | print ( "ready to go" ) |
03 | local playergui = game.Players.LocalPlayer.PlayerGui |
04 | local TextButton = playergui.ScreenGui.TextButton |
05 | local randomtime = math.random( 2 , 4 ) |
06 |
07 | wait(randomtime) |
08 |
09 | TextButton.BackgroundColor 3 = Color 3. new( 0 , 225 , 0 ) |
10 | TextButton.Text = "Click!" |
11 |
12 | local startTick = tick() |
13 |
14 | TextButton.Activated:Connect( function () |
15 | TextButton.Text = string.format( ".2%f" ,tick() - startTick) |
16 | end ) |