Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make TextButton stop displaying 0?

Asked by 4 years ago
Edited 4 years ago

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:

01wait(2)
02print("ready to go")
03local playergui = game.Players.LocalPlayer.PlayerGui
04local TextButton = playergui.ScreenGui.TextButton
05local randomtime = math.random(2,4)
06 
07wait(randomtime)
08 
09TextButton.BackgroundColor3 = Color3.new(0,225,0)
10TextButton.Text = "Click!"
11 
12TextButton.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
20end)

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

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().

01wait(2)
02print("ready to go")
03local playergui = game.Players.LocalPlayer.PlayerGui
04local TextButton = playergui.ScreenGui.TextButton
05local randomtime = math.random(2,4)
06 
07wait(randomtime)
08 
09TextButton.BackgroundColor3 = Color3.new(0,225,0)
10TextButton.Text = "Click!"
11 
12--start loop here
13local i
14local clicked = false
15for i = 0, math.huge, 1 do
View all 25 lines...

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.

01wait(2)
02print("ready to go")
03local playergui = game.Players.LocalPlayer.PlayerGui
04local TextButton = playergui.ScreenGui.TextButton
05local randomtime = math.random(2,4)
06 
07wait(randomtime)
08 
09TextButton.BackgroundColor3 = Color3.new(0,225,0)
10TextButton.Text = "Click!"
11 
12local startTick = tick()
13 
14TextButton.Activated:Connect(function()
15        TextButton.Text = string.format(".2%f",tick() - startTick)
16end)
Ad

Answer this question