One of the easiest ways to make your Roblox game feel more alive and immersive is by adding a day/night cycle. Whether you’re building a survival game, a roleplay experience, or just want to add some atmosphere, a dynamic sky that transitions from sunrise to sunset can make a huge difference!
In this tutorial, we’ll walk through creating a smooth day/night cycle using Roblox’s built-in Lighting service. Don’t worry if you’re new to scripting—this is a great beginner project that will teach you about services, properties, and loops.
Understanding the Lighting Service
Roblox has a built-in service called Lighting that controls how your game world looks. The most important property for our day/night cycle is ClockTime, which represents the time of day in hours (0-24). When you change this value, the sky, ambient lighting, and overall atmosphere update automatically.
Think of ClockTime like a clock:
- 0 = midnight
- 6 = sunrise
- 12 = noon
- 18 = sunset
- 24 = midnight again
By continuously increasing this value in a loop, we can create a smooth day/night cycle!
Creating Your First Day/Night Cycle
Let’s start with a basic script. We’ll place this in ServerScriptService so it runs for everyone in the game.
Step 1: Set Up Your Script
First, create a new Script in ServerScriptService (not a LocalScript—we want this to run on the server so all players see the same time of day).
Step 2: Write the Basic Code
Here’s a simple script that creates a day/night cycle:
local Lighting = game:GetService("Lighting")
while true do
Lighting.ClockTime += 0.1
wait(0.1)
end
Let’s break down what’s happening here:
local Lighting = game:GetService("Lighting")gets a reference to the Lighting service so we can modify its propertieswhile true docreates an infinite loop that will run foreverLighting.ClockTime += 0.1increases the time by 0.1 hours (6 minutes in-game time)wait(0.1)pauses for 0.1 seconds before the next update
If you run this script, you’ll see the sky start moving through the day! But there’s a problem—when ClockTime goes past 24, it keeps counting up (25, 26, 27…) instead of looping back to 0.
Step 3: Make It Loop Properly
We can fix this using the modulo operator (%), which gives us the remainder after division. Here’s the improved version:
local Lighting = game:GetService("Lighting")
while true do
Lighting.ClockTime = (Lighting.ClockTime + 0.1) % 24
wait(0.1)
end
Now when ClockTime reaches 24, it automatically wraps back to 0, creating a seamless loop!
Customizing Your Day/Night Cycle
The basic cycle works, but you might want to adjust how fast it runs. The speed depends on two things:
- How much you increase ClockTime (the 0.1 in our code)
- How long you wait between updates (the wait duration)
Making a Configurable Cycle
Here’s a more flexible version where you can easily adjust the cycle length:
local Lighting = game:GetService("Lighting")
local minutesPerDay = 10 -- One full day/night cycle takes 10 minutes
local hoursPerSecond = 24 / (minutesPerDay * 60)
while true do
Lighting.ClockTime = (Lighting.ClockTime + hoursPerSecond) % 24
wait(1)
end
In this version, we calculate exactly how much to increase ClockTime each second based on how long we want a full day to last. If you set minutesPerDay to 10, a complete day/night cycle will take 10 real-world minutes. Set it to 5 for a faster cycle, or 20 for a slower, more realistic one!
Adding Extra Polish
Want to make your cycle even better? Try adjusting these Lighting properties:
Lighting.Brightness– How bright the overall lighting isLighting.OutdoorAmbient– The color tint of outdoor areasLighting.Ambient– The color tint of all lighting
You can change these values based on the ClockTime to create more dramatic sunsets or darker nights. We’ll cover advanced lighting techniques in future tutorials!
Recap and Next Steps
Congratulations! You’ve just created a working day/night cycle. Here’s what you learned:
- How to use the Lighting service and its
ClockTimeproperty - How
while true doloops work for continuous updates - How to use the modulo operator (
%) to create repeating cycles - How to calculate timing for your desired cycle length
Now that you have a basic day/night cycle working, try experimenting! What happens if you make the nights much longer than the days? Can you add a GUI that shows players what time it is?
What to learn next: Try creating a script that changes the sky’s appearance during different times of day, or look into tweening Lighting properties for smooth color transitions during sunrise and sunset!