You ever see games with a cool day and night cycles, and want one in you’re game, but oof, you don’t know how to make one and the ones in the toolbox are bad messy codes/ filled with viruses? No worries. I got you covered. I made a simple day and night cycle with some customizability including: How long the day is (in minutes) and how long the night is (in minutes).
– SETUP -----------------------------------------------------------------------------------------------------------
To set this up: Insert a script inside ServerScriptService, then drag over all the code and CTRL-C. Then go to the script in ServerScriptService, remove the Hello world line and CTRL-V the code. Then you may modify the time for day and night. Have fun~!
– CODE -------------------------------------------------------------------------------------------------------------
local TimePerDay = 0.5 -- The time in daylight (Minutes)
local TimePerNight = 0.5 -- The time in nighttime (Minutes)
-- Variables/Calculation
local Seconds
local TimePassedBySeconds
local Lighting = game:GetService("Lighting")
-- Code
while game:GetService("RunService").Heartbeat:Wait() do -- A loop running 60 times per second to smoothly make the daylight system
if Lighting:GetMinutesAfterMidnight() >= 360 then -- For daytime
Seconds = TimePerDay*7200
TimePassedBySeconds = 24/Seconds
end
if Lighting:GetMinutesAfterMidnight() >= 1080 then -- For nighttime
Seconds = TimePerNight*7200
TimePassedBySeconds = 24/Seconds
end
Lighting.ClockTime += TimePassedBySeconds -- Adds the calculation to the time so it becomes a daylight system
end
Here is a showcase:
No credit is needed, its simple code.
EDIT: Fixed an issue where 1 minute was 30 seconds