Ways to Improve this Time Code?

Hi,

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

For looping around the hours, I would recommend using the modulo operator % as this will keep looping around at 24, 48, etc.

hrs %= 24

Same thing for minutes, but with 60.
Also, you can use string.format which adds extra padding zeros before your number.

timeString = string.format("%02i:%02i", hrs, mins)

I get slightly confused on how that works, I know what It does, it but i still get confused.

What exactly is this? Is it basically %02i where it does 02? (Me dumb)

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.

2 Likes

Thanks, but although Hrs %= 24 helps, it says 24:00 instead of 00:00, how would I fix this?

If Hrs is 24, it should display 0, as 24 / 24 is 1 with remainder 0. Can you post the code you currently have?

nevermind, I placed it before changing the hours, it works tho, thanks!

This is how it looks btw

function mSvc:NewTime()

	if mSvc.Min > 59 then
		mSvc.Hour += 1
	end
	
	mSvc.Hour %= mSvc.HourLimit
	mSvc.Min %= mSvc.MinLimit
	
	mSvc.TweenService:Create(game.Lighting, TweenInfo.new(.9), {ClockTime = mSvc.Hour + mSvc.Min/100}):Play()
	return string.format("%02i:%02i", mSvc.Hour, mSvc.Min)
end

@ElusiveEpix also, good job on the 50

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.