Issue with timer

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
1 Like

So what do you want to make because I don’t understand what you mean

Instead of using a while true loop, you should use a .changed event because when the player dies, there might be some errors.
Example:

BoolVal.Changed:Connect(function()
	if BoolVal == true then
		Timer.TextColor3 = Color3.fromRGB(50,50,50)
		Timer.Visible = true
		Timer.Text = Time.."s"
		Time = Time + 0.1
	elseif BoolVal == false
		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?

Im trying to fix the issue where it will randomly become 5.9999 or something like that instead of 5.9.

You’re right, let me fix the code. Edit: Okay I fixed it

What do you mean when you say keep 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.

Heres a video of it:

I don’t know much about the “Time” variable and also how or how fast the BoolValue is being set to true

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

Oh wow, that worked perfectly, will it still be 100% accurate though?

It should be, if there’s issues with it you’re always free to contact me.

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.