Hey everyone! So I made a day and night cycle system that tells you the AM/PM, day of the week, hour, minute, and second. I could’t find a good one so I just made one myself and I want to show you how to do it too!
I was really inspired by how clean and pretty Welcome to Bloxburg’s [Created by @coeptus] day and night cycle worked so I made a very similar one.
My previous day/night systems were pretty basic, just a loop that adds 0.1 to lighting.ClockTime. I personally thought this wasn’t enough and looked really bad. This module changes the sun’s position every 3 hours and looks really clean. This can potentially increase your player’s engagement in your game!
If you don’t wanna learn how its made, just take this model and stop reading or keep reading and learn how to do it yourself!
First I just add a ModuleScript to ReplicatedStorage and add the main system
function module:StartTime()
while true do
for d = 0,6,1 do --Days of the week
for h = 1,24,1 do --Hours per day
for m = 0,59,1 do --Minutes Per Hour
for s = 0,59,1 do --Seconds per minute
RunService.Heartbeat:Wait()
end
end
end
end
end
end
Next add these couple functions and variables! I'll explain what they do in the image!
local module = {}
-- Essential Variables --
local MA = "AM"
local RunService = game:GetService("RunService")
local LG = game:GetService("Lighting")
local sunTime = 0
------------------------
local function ChangeAM(num) --This function checks if the current period of the day is AM or PM
if num <= 12 then
MA = "AM"
elseif num > 12 then
MA = "PM"
end
if num % 3 == 0 then --This checks to see if the hour is divisible by 3 w/o a remainder, if it is it moves the sun's position
for i = sunTime,num,0.05 do
RunService.Heartbeat:Wait()
sunTime = i
LG.ClockTime = sunTime
end
end
end
local function FormatHour(num) --This function chenges the time from military time to standard format
if num > 12 then
return tostring(num - 12)
else
return tostring(num)
end
end
local function FormatNumber(num) --Formats numbers if they are under 10 then it would put a 0 in front of the number for a more readable format [EX. 00,01,...,08,09]
if num <= 9 then
return "0"..tostring(num)
else
return tostring(num)
end
end
local function FormatDay(num) --Converts the number [Day] to a more readable value
num = num + 1
local days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
return days[num]
end
Now just add the functions to the main function and you are done!
function module:StartTime()
while true do
for d = 0,6,1 do
for h = 1,24,1 do
ChangeAM(h) --Run this function every hour, see above for how it works
for m = 0,59,1 do
for s = 0,59,1 do
local DateTime = FormatHour(h)..":"..FormatNumber(m)..":"..FormatNumber(s).." "..MA.." "..FormatDay(d) --Vairable for concatenating the data
print(DateTime)--Print the data
RunService.Heartbeat:Wait() --Put a wait(1) here for realistic time
end
end
end
end
end
end
return module
Now just put this code in a Server script and click play on your game!
require(game:GetService("ReplicatedStorage").DayNightModule):StartTime()
Output should look like this!
Thank you for taking the time to read this and I hope this helps you on your quest to become a big time developer!
Here is the model again so you don’t have to scroll up. Any thoughts or improvements you think would be nice, please don’t be afraid to ask in the comments!