Easy Written By airkiller2222

Basic Daylight Cycle Created on: 01-01-2019

I will show you how to make a Daylight Cycle

I will be showing you how to make a basic Daylight Cycle. I will show you real-time first, then 1 in-game minute per real-time second, and 1 in-game hour per real-time second. Let's get started, shall we?

Step 1: Creating the Script

The first step for running a daylight cycle is to be able to code it first. Put a Script in either Workspace or ServerScriptService, and preferably without any parents exept from Folders. Name the script whatever you want, but make it easy to identify in case you screw something up and it glitches out.

Step 2: Establishing Variables

As with all programming, one of the first steps is to make your variables. This will allow you to make your code to access certain objects without the difficulty of rewriting it every time. You need 1 variable that is named with any name. I will choose 'minutesAfterMidnight' because it's easier to understand. Set the value of it to 0 for now.

local minutesAfterMidnight = 0

Step 3: Looping

You want the time to actually change, right? You will need a 'while true do' loop with no exit parameters so you don't have to write a peice of code every time you want the time to change, which is tedious and impractical. You should also have an 'end' statement 2 lines down so you don't get an error basically saying "Where's this loop's code supposed to end?"

local minutesAfterMidnight = 0
while true do

end

Step 4: Changing the Time

Time for the fun stuff. You want to make your code access the 'Lighting' portion of the game to change the 'SetMinutesAfterMidnight' value. To do this, write 'game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)'. This will set the time to 12:00 AM, midnight. Now, we want to change it. I like do go with realistic time changing because I'm weird and I find slow progression satisfying. Put underneath the code I mentioned earlier 'minutesAfterMidnight = minutesAfterMidnight + .0167', so it only adds 1 second every time.

local minutesAfterMidnight = 0
while true do
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	minutesAfterMidnight = minutesAfterMidnight + .0167
end

Step 5: Not Breaking Studio

Now, if we run this, Studio will lag out and probably crash. That's what 'wait' commands are for. If we change the minutesAfterMidnight by about 1 second, or .0167 of a minute, we have to wait one second for it to be real-time. Under the new code, but still in the 'while true do' function, write 'wait(1)'. Now, you have a real-time daylight cycle!

local minutesAfterMidnight = 0
while true do
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	minutesAfterMidnight = minutesAfterMidnight + .0167
	wait(1)
end

Shorter Days/Nights

I know for a lot of you, it's tedious to try and watch a real-time sunrise for what feels like an eternity. So, you can change the minutesAfterMidnight to the hour you want (like 7) and multiply it by 60. If you want it to go by faster, you can change the 'minutesAfterMidnight = minutesAfterMidnight + .0167' .0167 variable to something like '1' for 1 minute every second, or '60' for one hour per second. You can also slow it down by changing it into a number less than .0167 for a slo-mo day/night cycle.

Easy Written By airkiller2222

See airkiller2222's profile on Roblox

Discussion