首页 > 解决方案 > 计时器脚本不循环(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

标签: luaroblox

解决方案


我看不出整体逻辑有任何问题,所以没有理由让它在 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

推荐阅读