So I’ve done some math to make this clock system but the way it works kinda makes me feel a little disorganized. I also feel like this could cause unneeded lag?
Module script code:
module.NightToDay = function(timeSet)
local starTime = 0 -- 12:00 AM in 24 hour format
local endTime = 8 * 60 -- 8:00 AM in minutes
local duration = 3 -- total duration in seconds
local stepsPerSecond = 20 -- number of steps per second
local totalSteps = duration * stepsPerSecond
local interval = (endTime - starTime) / totalSteps
local info = TweenInfo.new(timeSet)
ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
local last = ts:Create(atmo,info,{Density = .3})
last:Play()
task.spawn(function() -- does the clock time
for currentTime = starTime,endTime,interval do
local hours = math.floor(currentTime / 60) % 12
hours = (hours == 0) and 12 or hours
local minutes = currentTime % 60
innerClock.Text = string.format("%02d:%02d",hours,minutes) -- changes the clock time
wait(1 / stepsPerSecond) -- waits the calculated time
end
end)
last.Completed:Wait()
return (last.PlaybackState == Enum.PlaybackState.Completed)
end
Put the variables that don’t change (constants) on top of the module, not inside the function. Use x // number instead of math.floor(x / number) You should also be using task.wait instead of wait.
Code:
local starTime = 0 -- 12:00 AM in 24 hour format
local endTime = 8 * 60 -- 8:00 AM in minutes
local duration = 3 -- total duration in seconds
local stepsPerSecond = 20 -- number of steps per second
local totalSteps = duration * stepsPerSecond
local interval = (endTime - starTime) / totalSteps
module.NightToDay = function(timeSet)
local info = TweenInfo.new(timeSet)
ts:Create(lighting, info, {ClockTime = 8, Brightness = 3}):Play()
local last = ts:Create(atmo, info, {Density = 0.3})
last:Play()
task.spawn(function() -- does the clock time
for currentTime = starTime, endTime, interval do
local hours = currentTime // 60 % 12
hours = (hours == 0 and 12) or hours
-- changes the clock time
local minutes = currentTime % 60
innerClock.Text = string.format("%02d:%02d", hours, minutes)
-- waits the calculated time
task.wait(1 / stepsPerSecond)
end
end)
last.Completed:Wait()
return last.PlaybackState == Enum.PlaybackState.Completed
end