So, I’m in the midst of making a game, and I’ve put together a script that calculates the time left in a week for uses in daily challenges, weekly challenges, and a weekly shop by setting the calculation to a formatted string value. My question is, will this loop cause any issues relating to performance, and is there any way to make it a bit better.
Here’s my code:
local rs = game:GetService('ReplicatedStorage')
local values = rs:WaitForChild("Values") -- Value folder in ReplicatedStorage
local weekTimeLeft = values:WaitForChild("WeekTimeLeft") -- String value for tracking the time
local modelSunday = os.time({year = 2020, month = 9, day = 27}) -- Reference point for a past sunday.
local function toDHMS(t)
return string.format("%02d:%02d:%02d:%02d", math.floor(t/60/60/24), math.floor(t/60/60%24), math.floor(t/60%60), math.floor(t%60))
end
local weekLength = 60 * 60 * 24 * 7
while wait(1) do
local goal = modelSunday
while goal < os.time() do
goal += weekLength
end
local timeLeft = goal - os.time()
if timeLeft > 0 then
weekTimeLeft.Value = toDHMS(timeLeft)
else
weekTimeLeft.Value = "00:00:00:00"
end
end
Thanks in advance!