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
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.