So I am currently looking for ways to Improve this code, to me, it looks like it can be done better, however I’m not sure if it can be simplified, or made shorter
To give you context, I have a game that uses this timer with Dynamic Lighting, thats all.
local mSvc = {}
mSvc.TweenService = game:GetService("TweenService")
mSvc.TimeValue = game.ReplicatedStorage.Content.CurrentTime
mSvc.DefaultTime = "00:00" -- Time (or string) the Game Starts on
mSvc.DayState = {"AM", "PM"} -- Idk what to call it (not used yet)
mSvc.Hour = 0 -- Hour
mSvc.Min = 0 -- Minutes
function mSvc:NewTime(hrs: number, mins: number)
local StrHrs
local StrMin
if hrs > 23 then hrs = 0 end -- if time is greater than 23, resets
if mins > 59 then mins = 0 hrs += 1 end -- if minutes is greater than 59
if hrs < 10 then StrHrs = "0"..hrs else StrHrs = hrs end -- Applys a Zero if number is less than 10
if mins < 10 then StrMin = "0"..mins else StrMin = mins end
mSvc.TweenService:Create(game.Lighting, TweenInfo.new(.9), {ClockTime = hrs + mins/100}):Play() -- Hour = 1 Minutes = .59
return StrHrs..":"..StrMin -- Returns Concat to use
end
return mSvc
Code firing the Module:
local Main = require(game.ServerStorage.Folder.Main)
Main.TimeValue.Value = Main.DefaultTime
while true do
Main.TimeValue.Value = Main:NewTime(Main.Hour, Main.Min)
task.wait(1)
Main.Min += 1
end
The modulo operator gets the remainder of a division, and it has the effect of “looping around” a certain modulus (in your case, 24 or 60). Below is a picture of a system with modulus 12. (We call it the AM-PM system)
See how if you go 4 hours past 9 o’clock, you get to “13 o’clock”, but we call it 1 o’clock.
In the string format, the % symbol means “this is something to be replaced”. The 0 then means that the number is to be left padded with zeros. The 2 gives the minimum length for the replacement, 2. i just stands for integer.