I'm currently writing a code in Roblox Studio (with the Lua language) and I'm making a timer, but the decimals randomly change from 3 to like 30. How can I fix this? Code:
我目前正在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
I wanted the timer to update every 0.125 seconds, but instead it works a few times but then the decimals randomly switch up from 3 to 30. FOR EXAMPLE I wanted it to be 2.250 and not 2.25099999999999999999999999987. How can I fix this?
我希望计时器每隔0.125秒更新一次,但它只工作了几次,然后小数从3随机切换到30。例如,我希望它是2.250,而不是2.2509999999999999999999999999987。我怎么才能解决这个问题呢?
更多回答
优秀答案推荐
When you display the time, use the string.format
function to format the number :
显示时间时,请使用字符串.Format函数来格式化数字:
Text.Text = string.format("%.3f", Time)
In this example, %f
is used to display a floating point number, and %.3f
limits the precision of that number to just 3 decimal points.
在本例中,%f用于显示浮点数,%.3f将该数字的精度限制为3个小数点。
For more information, see this answer : https://stackoverflow.com/a/1811889/2860267
有关更多信息,请参阅以下答案:https://stackoverflow.com/a/1811889/2860267
local player = game.Players.LocalPlayer
local Running = player:WaitForChild("Running")
--
local Text = script.Parent:WaitForChild("Time")
Text.Text="0.00"
Text.Visible=true
local tab={
[true]=function()
Text.TextColor3=Color3.fromRGB(232,232,232)
while(wait(.125)and Running.Value)do
Text.Text+=(.125)..[[
]]
end
end,
[false]=function()
Text.TextColor3=Color3.fromRGB(255,47,92)
Text.Text='0.00'
end
}
Running.Changed:Connect(function(v)
tab[v]()
end)tab[Running.Value]()
更多回答
我是一名优秀的程序员,十分优秀!