My seconds in my countdown scripts go slower that the actual seconds go. I tried adding deciseconds but that didn’t help. I tried even milliseconds but still no luck.
local timer = script.Parent
local minutes = script.Parent.Minutes.Value
local seconds = 0
repeat
if seconds <= 0 then
seconds = 59
minutes = minutes - 1
else
seconds = seconds - 1
end
if seconds <= 9 then
timer.Text = "[ "..tostring(minutes)..":0"..tostring(seconds).." ]"
else
timer.Text = "[ "..tostring(minutes)..":"..tostring(seconds).." ]"
end
wait(1)
until minutes <= 0 and seconds <= 0
Dont rely on timings from wait to be perfect, instead use a frame based system and the return from task.wait to better determine how much longer to wait
local timeRemaining = 5
while timeRemaining > 0 do
timeRemaining -= task.wait()
text = math.floor(timeRemaining)
end
And, you can format your minutes:seconds from a single second variable using the % operator
local timeRemaining = 96
local minutes = math.floor(timeRemaining / 60) % 60
local seconds = timeRemaining % 60
--!strict
local rate: number = 1 -- 1 times a second
local accumulated: number = 0
game:GetService("RunService").Heartbeat:Connect(function(deltaTime: number)
accumulated += deltaTime
while accumulated >= rate do
accumulated -= rate
-- Main logic goes here.
end
end)
Sorry I am not really that much of a scripter. Is this right?
local timeRemaining = 240
local minutes = math.floor(timeRemaining / 60) % 60
local seconds = timeRemaining % 60
while timeRemaining > 0 do
timeRemaining -= task.wait(1)
if seconds < 9 then
script.Parent.Text = "[ "..tostring(minutes)..":0"..tostring(seconds).." ]"
else
script.Parent.Text = "[ "..tostring(minutes)..":"..tostring(seconds).." ]"
end
end
Here you go, I hope this is to your liking. I performance checked your version of the script against lag and seems like it doesn’t do a good job at it.
--!strict
local textObject: any = script.Parent.textObject
local timeRemaining: number = 240
local rate: number = 1 -- 1 times a second
local accumulated: number = 0
game:GetService("RunService").Heartbeat:Connect(function(deltaTime: number)
accumulated += deltaTime
while accumulated >= rate do
accumulated -= rate
if timeRemaining > -1 then
textObject.Value = string.format(
'[%d:%d]',
math.floor(timeRemaining * .0166666667),
timeRemaining % 60
)
else
print("TIME RAN OUT! Resetting timer...")
timeRemaining = 240
end
timeRemaining -= 1
end
end)
Nope, this is the local script I have in my text label. Also the text label is not in starter gui. Yeah I forgot to mention it’s in workspace in a part.
local textObject: any = script.Parent
local timeRemaining: number = script.Parent.TimeRemaining
local rate: number = 1 -- 1 times a second
local accumulated: number = 0
game:GetService("RunService").RenderStepped:Connect(function(deltaTime: number)
accumulated += deltaTime
while accumulated >= rate do
accumulated -= rate
if timeRemaining.Value > -1 then
textObject.Text = string.format(
'[%d:%d]',
math.floor(timeRemaining * .0166666667),
timeRemaining % 60
)
else
print("TIME RAN OUT! Resetting timer...")
timeRemaining = 240
end
timeRemaining -= 1
end
end)
Yeah I already found the solution, thanks for your help and have a nice day. Sorry for taking so long. Also one little question you could answer for me: How would I put a 0 before the seconds when they are less then 9?
For that I’d turn the modulus calculation into a variable, since it’s going to get referenced twice, and same with formatString since it’s going to change when the seconds are below 10 (I assume below 10 is what you meant).
I also gave you a max time value, so it’s easier to manage for example when the value gets reset:
Here’s the full script:
--!strict
local textObject: any = your directory goes here
local MAX_TIME_DEFAULT: number = 240
local timeRemaining: number = MAX_TIME_DEFAULT
local rate: number = 1 -- 1 times a second
local accumulated: number = 0
game:GetService("RunService").Heartbeat:Connect(function(deltaTime: number)
accumulated += deltaTime
while accumulated >= rate do
accumulated -= rate
if timeRemaining > -1 then
local seconds = timeRemaining % 60
local formatString = '[%d:%d]'
if seconds < 10 then
formatString = '[%d:0%d]'
end
textObject.Text = string.format(
formatString,
math.floor(timeRemaining * .0166666667),
seconds
)
else
print("TIME RAN OUT! Resetting timer...")
timeRemaining = MAX_TIME_DEFAULT
end
timeRemaining -= 1
end
end)