定时器脚本不循环(Roblox Lua)

Timer script does not loop (Roblox Lua)

计时器脚本在 2:29 处停止并且不会从那里开始倒计时。它应该倒计时到零,但会在 1 个循环后停止。 while true do 循环继续运行,但要么文本标签不显示它,要么分钟和秒变量没有改变。我需要帮助才能完成这项工作。

local starterGui = game:GetService("StarterGui")
local Guis = starterGui.RoundTimer --Includes the time textlabel.
local Seconds = 30
local Minutes = 2

repeat
    wait(1)
    if Seconds < 9 then
        if Seconds == 0 then
            Seconds = 59
            Minutes = Minutes - 1
        else
            Seconds = Seconds - 1
        end
        Guis.Time.Text = tostring(Minutes)..":0"..tostring(Seconds)
    else
        Seconds = Seconds - 1
        Guis.Time.Text = tostring(Minutes)..":"..tostring(Seconds)
    end
until Seconds < 1 and Minutes < 1

我没有发现整体逻辑有任何问题,因此没有理由在 2:29 处停止,但格式存在一些问题,因为这是我在 [=19] 时得到的结果=] 脚本(片段):

1:10
1:9
1:8
1:07
1:06
1:05
1:04
1:03
1:02
1:01
1:00
0:059
0:58

如您所见,:8、:9 和 :059 的格式不正确。

像这样的东西可能会更好一些:

repeat
    Guis.Time.Text = ("%d:%02d"):format(Minutes, Seconds)
    wait(1)
    Seconds = Seconds - 1
    if Seconds < 0 then
      Minutes = Minutes - 1
      Seconds = 59
    end
until Seconds < 1 and Minutes < 1

我现在已经知道了一点,但万一有人想知道是什么起作用了:

while true do
    wait(1)
    local timeleft = game.ReplicatedStorage.Seconds.Value -- the seconds value
    local minutes = math.floor(timeleft/60) -- getting the amount of minutes by dividing seconds by 60
    local seconds = timeleft%60 -- gets the remaining seconds by using modulo 60 with time left.

    script.Parent.Text = string.format("%d:%02d", minutes%60, seconds) -- formats this into a timer.
end

有关 script.format() 的更多信息可在此处找到:https://developer.roblox.com/en-us/articles/Format-String

%/取模如何工作: 假设你有 90 秒,想把它变成 1:30。您已经知道通过使用 timeleft/60 可以计算出时钟还有一分钟。使用 timeleft%60 将剩余时间除以 60,然后 returns 余数,因此得到 30。