How can I get this countdown to stop at 0?

I followed a tutorial for a stopwatch countdown and its pretty straightforward. However, I can’t figure out how to get it to stop at 0 every time. The “if label = text 0” only works if theres little lag. what can i replace it with? i’m new to strings also.

heres the script.

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local timerEvent = ReplicatedStorage:WaitForChild(“TimerEvent”)
local timeout = ReplicatedStorage:WaitForChild(“timerranout”)

local label = script.Parent
local screenGui = label.Parent
local running = false
local currentTime = 10

screenGui.Enabled = false

timerEvent.OnClientEvent:Connect(function(action)
if action == “Start” then
if not running then
screenGui.Enabled = true
running = true
currentTime = 10
task.spawn(function()
task.wait(1)
while running do

				if label.Text == string.format("0.0") then   --this works when no lag 
					
					print("timer stops")
					running = false
					screenGui.Enabled = false
					timeout:FireServer()
					break
				else
				currentTime = currentTime - task.wait(0.1)
				label.Text = string.format("%.1f", currentTime)
				end
			end
		end)
	end

	

elseif action == "Stop" then
	if running then
		running = false
		task.wait(3)
		if not running then
			screenGui.Enabled = false
			label.Text = "10"
		end
	end
end

end)

1 Like

I think you might be running into the issue due to you having the EXACT “0.0” as task.wait(0.1) waits approximately 0.1 instead of exactly.

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local timerEvent = ReplicatedStorage:WaitForChild(“TimerEvent”)
local timeout = ReplicatedStorage:WaitForChild(“timerranout”)

local label = script.Parent
local screenGui = label.Parent
local running = false
local currentTime = 10

screenGui.Enabled = false

timerEvent.OnClientEvent:Connect(function(action)
    if action == “Start” then
        if not running then
            screenGui.Enabled = true
            running = true
            currentTime = 10
            task.spawn(function()
            task.wait(1) 
            while running do
				if currentTime <= 0 then   --this works when no lag 
					print("timer stops")
					running = false
					screenGui.Enabled = false
					timeout:FireServer()
					break
				else
				    currentTime = currentTime - task.wait(0.1)
				    label.Text = string.format("%.1f", currentTime)
				end
			end
        end
-- Rest of your code

I think it would be easier to use RunService and DeltaTime but this should fix it for now.

1 Like

i assume there’s a server side, that fires “start” and “stop”
the thing is you can’t make it precise, because network delays, client delays and so on

so i suggest to make it stop by itself on client side:

  1. while currentTime ~= 0 and running
  2. currentTime = math.max(0, currentTime - task.wait(0.1)) so the currentTime never becomes negative

also next time please pay attention to formatting, it would make easier to read the source.
thank you!

1 Like

Thank you so much, I was really stuck​:sweat_smile::folded_hands:t4:

1 Like

You’re totally right about the formatting, sorry! It’s my first post I wasn’t really sure how to fix it

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