计时器脚本在Roblox Lua中不循环

计时器脚本在2:29停止,不会继续倒数。它应该倒数到零,但在循环1次后会停止。while true do循环继续,但文本标签可能不会显示它,或者分钟和秒钟变量没有改变。我需要帮助让它工作。

local starterGui = game:GetService("StarterGui")
local Guis = starterGui.RoundTimer --包括时间文本标签。
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
点赞
用户1442917
用户1442917

我认为总体逻辑没问题,所以没有理由在2:29停止,但格式方面有些问题,当我运行脚本时(一个片段),我得到了以下结果:

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
2021-07-20 18:43:20
用户13855913
用户13855913

我已经知道这一点了,但如果有人想知道怎么做:

while true do
    wait(1)
    local timeleft = game.ReplicatedStorage.Seconds.Value -- 获取秒数值
    local minutes = math.floor(timeleft/60) -- 通过将秒数除以60获取分钟数
    local seconds = timeleft%60 -- 通过使用60除余时间,获取剩余的秒数。

    script.Parent.Text = string.format("%d:%02d", minutes%60, seconds) -- 将分钟和秒数格式化成计时器。
end

有关string.format()的更多信息请看这里:https://developer.roblox.com/en-us/articles/Format-String

% /除余运算 的作用: 比如说你有90秒,并想要将其转换成1分30秒。你已经找出了通过时间left/60可以得出现在是一分的。通过使用timeleft%60将时间left除以60后得到余数,因此得到了30。

2022-01-17 03:45:46