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:


wait(2) print("ready to go") local playergui = game.Players.LocalPlayer.PlayerGui local TextButton = playergui.ScreenGui.TextButton local randomtime = math.random(2,4) wait(randomtime) TextButton.BackgroundColor3 = Color3.new(0,225,0) TextButton.Text = "Click!" TextButton.Activated:Connect(function(activated) for i = 0, math.huge, 1 do wait() if activated then TextButton.Text = (i) break end end end)

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

wait(2)
print("ready to go")
local playergui = game.Players.LocalPlayer.PlayerGui
local TextButton = playergui.ScreenGui.TextButton
local randomtime = math.random(2,4)

wait(randomtime)

TextButton.BackgroundColor3 = Color3.new(0,225,0)
TextButton.Text = "Click!"

--start loop here
local i
local clicked = false
for i = 0, math.huge, 1 do
    wait()
    if clicked then
        break
    end
end

TextButton.Activated:Connect(function()
    clicked = true
        TextButton.Text = to.string(i)
end)

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.

wait(2)
print("ready to go")
local playergui = game.Players.LocalPlayer.PlayerGui
local TextButton = playergui.ScreenGui.TextButton
local randomtime = math.random(2,4)

wait(randomtime)

TextButton.BackgroundColor3 = Color3.new(0,225,0)
TextButton.Text = "Click!"

local startTick = tick()

TextButton.Activated:Connect(function()
        TextButton.Text = string.format(".2%f",tick() - startTick)
end)
Ad

Answer this question