How to Script a Day and Night Cycle in Roblox

The completed Day/Night cycle by the end of this article

Day/Night Cycles are a feature in Roblox games, where the sky gets darker as the time passes, and brighter as morning starts. In this article, I’ll teach you how to script your very own awesome day and night cycle for your Roblox game.

To begin with, you’ll want to download Roblox Studio and make sure that you can see the Explorer and Properties windows.

How Time Works in Roblox

In order to script a day and night cycle on Roblox, we have to first understand how time is handled in Roblox. So, let’s start there.

Lighting

Inside of your Explorer window, look for the Lighting service.

The Lighting service in the Explorer
The Lighting Service on Roblox is represented by a lightbulb, and is located right below Players.

Lighting is a service on Roblox Studio that handles the lighting in your game.

For example, with Lighting, you can change things like the shadow softness in your game, ambience, brightness, add surrounding fog, etc.

And yes, one of the things you can do with Lighting is change the time of day.

In Roblox, time is handled similarly to how you and I think of time.

The TimeOfDay Property of the Lighting

If you scroll down in the Properties window, you can see that there’s a property called TimeOfDay.

The TimeOfDay property
The TimeOfDay is in military time.

By default, the TimeOfDay is set to 14:30:00.

This might seem confusing to you. I mean, 14 o’clock!?

Don’t worry. It’s written in military time, which means that instead of having AM and PM, it just exceeds the “12:00” mark.

So, 14:30:00 in military time is just 2:30 PM.

For you to understand how the TimeOfDay property works in Roblox, it’d probably help to understand a bit more about military time.

Converting From Military Time to 24 Hour Clock Time

So, let me help you convert military time to something you’re more familiar with (24 Hour Clock Time)

In order to convert from military time to 24 hour clock time, use this rule:

If the military time < 12, the clock time is the military time (AM).

If military time >= 12 and military time < 24, the clock time is the military time – 12 (PM)

If the military time is 24, then the clock time is 12:00 PM.

Confusing? Don’t worry. I’ll help you understand.

Let’s look at a few examples:

Convert 6:00 to 24 Hour Clock Time

Using the rule above, 6 is less than 12, so 6:00 in military time is the same as 6:00 AM.

Convert 9:00 to 24 Hour Clock Time

Using the rule above, 9 is less than 12, so 9:00 in military time is the same as 9:00 AM.

Convert 14:00 to 24 Hour Clock Time

This one’s a bit tricky. But we can do it!

14 is larger than 12, but less than 24, so the clock time is is 14 – 12 = 2:00 PM.

Convert 18:00 to 24 Hour Clock Time

Using the same rule, 18 is in between 12 and 24.

So, we subtract it by 12.

18 – 12 = 6:00 PM.

Hopefully that made a bit more sense.

If not, here’s a chart to help.

Understanding Time of Day in Roblox

Illustrating the Time of Day property to script a Day and Night cycle on Roblox

Now, if I told you to set the time to 6 AM, using the rule above, you know that 6 is less than 12, so you can just set the TimeOfDay to 6:00:00.

And suddenly, it got pretty dark, didn’t it?

What if you set it to 7 AM? Since 7 is less than 12, you can just set the TimeOfDay to 7:00:00.

Illustrating the Time of Day property to script a Day and Night cycle on Roblox

It gets brighter!

Pretty cool, huh?

So, just like in real life, we can alter the time of day property, and Roblox changes our scene to match.

From these basic observations, I think you can already tell where this is heading..

To make a Day/Night cycle, we just have to tell Roblox to increase the time every second!

The ClockTime Property

Now, in case the above observation didn’t immediately jump out at you, I’m going to show you something really cool.

So, get excited. This is going to be awesome.

Lighting also has a property called ClockTime.

ClockTime works just like the TimeOfDay property, except it’s a number representing the hour.

So, if we set ClockTime to 6, then it’s 6:00 AM.

If we set ClockTime to 18, it’s 6:00 PM.

And you know the best Part?

There’s a slider!

ClockTime is a property of the Lighting service that changes the time in your Roblox game

Go ahead. Let that inner kid out.

Slide the slider and observe the effects of increasing the ClockTime on your game.

The effects of changing the ClockTime on the lighting of your game

As you can see, as the ClockTime increases, the time in your day also increases.

And now, we have all of the Roblox Studio knowledge we need to begin scripting!

Scripting Your Own Day and Night Cycle

Awesome, so now, I’ll teach you how to script your very own day and night cycle.

Let’s get started by creating a script.

Add a Script into ServerScriptService.

A Script has been added into ServerScriptService

Now, if scripting is confusing to you, don’t worry. I’ve been teaching it for the past 9 years, so I’ll explain everything in a way where you can understand.

Remember the big idea from earlier?

To make a Day/Night cycle, we just have to tell Roblox to increase the time every second!

That’s exactly what we’re going to do.

While Loops

We need to tell Roblox “I want you to do something every second”

To do this, we can use something called a while loop.

This is a while loop that tells Roblox to do something every 1 second.

The task.wait(1) means “1 second”

The while task.wait(1) do means “Every 1 second, do this”

Now, what exactly do we want Roblox to do? Spawn 1,000 monkeys?

No, we want Roblox to increase the ClockTime property by 1.

And that’s exactly what this code will do:

Once you’ve put that above code in your script, you can click play, and marvel as you have a working day and night cycle!

Our Day and Night cycle

Improving the Day and Night Cycle

Cool!

Now, we can actually make this day and night cycle even better by using a technique called linear interpolation to our advantage.

Basically, what we’re going to do is instead of increasing the time by 1 hour every second, we’re going to increase the time by 1/10 of an hour every 0.1 seconds.

This is going to make our Day and Night cycle 10x smoother, because now, instead of it suddenly passing an hour in our game every second, it’ll pass by 1/10 of an hour every 1/10 of a second.

That’s about 6 minutes every 0.1 second.

And our Day and Night cycle will be even smoother.

A smoother version of the day and night cycle, using a technique called linear interpolation

Theoretically, we could make our original Day and Night cycle 100x smoother by changing the time by 1% of an hour every 0.01 second.

This will work, and the results will be super smooth!

Except, this smoothing technique can cause issues for our players on low-end devices, because we’re essentially telling Roblox “change the time of day extremely often.”

But there you have it!

If you liked the way I taught you about day and night cycles in this thread, you might be interested in my book, the Beginner’s Guide to Roblox Scripting, where you’ll learn how to script from scratch.

That’s all I have for today!

Thank you very much for reading, and make sure to subscribe to Kushal Writes, a free newsletter where you will get awesome Roblox articles like this one delivered to your inbox.

kushal Avatar

About the author

Subscribe to Kushal Writes

Awesome tips for game development, computer science, and life lessons to help you succeed — straight to your email for FREE!

Join 50+ readers

    By submitting this form, you’ll be signed up to my free newsletter. You can unsubscribe at any time.

    Learn Roblox Scripting

    Get the Beginner’s Guide to Roblox Scripting Book and learn Roblox scripting in an easy to understand way.



    Search Kushal Writes