Skinstashery
Roblox
Home / Roblox / Studio script library / Day and night cycle
RB-004

Roblox day and night cycle script

Moves the sun and moon so the game goes through a full day over a set time.

Script type
Script
Where it goes
ServerScriptService
Category
World and effects

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Day and night cycle: changes Lighting.ClockTime from 0 to 24 over and over.

local DAY_LENGTH_SECONDS = 600   -- real seconds for one full 24 hour game day
local START_HOUR = 8             -- the hour the game starts at (0 to 24)
local STEP_SECONDS = 0.25        -- how often the clock updates

local Lighting = game:GetService("Lighting")

Lighting.ClockTime = START_HOUR

local hoursPerStep = 24 / DAY_LENGTH_SECONDS * STEP_SECONDS

while true do
	Lighting.ClockTime = (Lighting.ClockTime + hoursPerStep) % 24
	task.wait(STEP_SECONDS)
end

How to set it up

  1. In the Explorer, find ServerScriptService.
  2. Click the plus button next to it and add a Script.
  3. Paste the code.
  4. Play. Watch the sky change. Try DAY_LENGTH_SECONDS = 60 to see it fast.

Change it

More in World and effects

All Roblox scripts

More Roblox tools