我目前正在 Roblox Studio(使用 Lua 语言)编写代码,并且正在制作计时器,但小数点随机从 3 变为 30 左右。我该如何解决这个问题?代码:
local player = game.Players.LocalPlayer
local Running = player:WaitForChild("Running")
--
local Text = script.Parent:WaitForChild("Time")
local Time = 0.00
while wait() do
if Running.Value == true then
Text.TextColor3 = Color3.fromRGB(232,232,232)
Text.Visible = true
Text.Text = Time
wait(0.125)
Time = Time + 0.125
else
if Running.Value == false then
Text.TextColor3 = Color3.fromRGB(255,47,92)
Time = 0.00
end
end
end
我希望计时器每 0.125 秒更新一次,但它会工作几次,但随后小数点随机从 3 切换到 30。例如,我希望它是 2.250,而不是 2.25099999999999999999999999987。我该如何解决这个问题?
显示时间时,使用
string.format
功能格式化数字:
Text.Text = string.format("%.3f", Time)
在此示例中,
%f
用于显示浮点数,%.3f
将该数字的精度限制为仅 3 位小数。
有关更多信息,请参阅此答案:https://stackoverflow.com/a/1811889/2860267