So I’m making a time trial game and I recently came across an issue with the display of the time. It works normally at first, adding 0.1 every 0.1 seconds but then it will suddenly switch to 5.999999 or something like that and i’m not really sure why. This is my script:
local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local Timer = PlayerGui:WaitForChild("TimerGui").Frame.Timer
local BoolVal = game:GetService("Players").LocalPlayer:WaitForChild("BoolStartEnd")
local Time = 0
while task.wait(0.1) do
if BoolVal.Value == true then
Timer.TextColor3 = Color3.fromRGB(50,50,50)
Timer.Visible = true
Timer.Text = Time.."s"
Time = Time + 0.1
elseif BoolVal.Value == false then
Timer.TextColor3 = Color3.fromRGB(0, 255, 0)
Time = 0
end
end
But how would the value get changed if the thing that is changing it, is inside the changed function? And how would this help keeping a simple decimal and not a random glitched one?
Well its supposed to go something like 5.1, 5.2, 5.3, 5.4, etc but it will suddenly become a huge decimal and I’m not really sure why since I’m only adding 0.1.
Assuming all you’re trying to do is round the number, you can do this,
local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local Timer = PlayerGui:WaitForChild("TimerGui").Frame.Timer
local BoolVal = game:GetService("Players").LocalPlayer:WaitForChild("BoolStartEnd")
local Time = 0
while task.wait(0.1) do
if BoolVal.Value == true then
Timer.TextColor3 = Color3.fromRGB(50,50,50)
Timer.Visible = true
Timer.Text = Time.."s"
Time = Time + 0.1
Time = math.round(Time * 10) / 10
elseif BoolVal.Value == false then
Timer.TextColor3 = Color3.fromRGB(0, 255, 0)
Time = 0
end
end
Since task.wait is not entirely accurate, you should try to lower the time wait a little bit:
local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local Timer = PlayerGui:WaitForChild("TimerGui").Frame.Timer
local BoolVal = game:GetService("Players").LocalPlayer:WaitForChild("BoolStartEnd")
local Time = 0
while task.wait(0.0978) do
if BoolVal.Value == true then
Timer.TextColor3 = Color3.fromRGB(50,50,50)
Timer.Visible = true
Timer.Text = Time.."s"
Time = Time + 0.1
Time = math.round(Time * 10) / 10
elseif BoolVal.Value == false then
Timer.TextColor3 = Color3.fromRGB(0, 255, 0)
Time = 0
end
end