Custom daylight cycle

Hello, I’m Tem and i’m bringing up to you a very easy day-cycle script.
with this script we can customize how fast the day would pass separately from how long the night would pass.
So the first thing you want to do is to get everything you need, in this case, Lighting service:

local lighting = game:GetService("Lighting")  -- getting the lighting service
local Clock = lighting.ClockTime  -- getting the clock time from lighting service

OK. Now that you’ve done that you need to set two variables for how fast the time would pass if it’s day and how fast it would pass if it’s night

local DayilightSpeed = 0.2 --this is the speed (time) that takes for the day to pass 
--(you can change however you wish(smaller numbers means less daylight duration))
local NightlightSpeed = 0.2 --this is the speed (time) that takes for the night to pass
--(same thing applies here too)

I set them equally, but if you want them to be different feel free to change that.
Now you need to do the main loop

while true do --this is the main loop, it will run constantly
	Clock = lighting.ClockTime -- this updates the time that's stored in the variable
end

In this case the Clock variable needs to be updated each time the loop runs. Alternatively you can use lighting.Clocktime every time the variable Clock appears.

Now we can add the part of the script that changes the time.

while true do
	Clock = lighting.ClockTime 
	if Clock > 6 and Clock < 20.02 then  --checks if the Clocktime is greater than 6 and less than 20.02
		--if so then it means that it's day outside
		wait(DayilightSpeed) -- here we wait the amount of time(in seconds) as set in the variable
		lighting.ClockTime = Clock + 0.001 -- here we change the time
	elseif Clock >= 20.02 or Clock <=6 then -- checks if the Clocktime is greater than 6
		--if so then it means that it's night outside
		wait(NightlightSpeed)-- here we wait the amount of time(in seconds) as set in the variable
		lighting.ClockTime = Clock + 0.001 -- here we change the time
	end
end

once again if you want the day pass differently from night then you can change these variables from here:

lighting.ClockTime = Clock + 0.001

for example if i want the day to pass faster than the night i can write the script like this:

while true do
	Clock = lighting.ClockTime 
	if Clock > 6 and Clock < 20.02 then  
		wait(DayilightSpeed)
		lighting.ClockTime = Clock + 0.1
	elseif Clock >= 20.02 or Clock <=6 then
		wait(NightlightSpeed)
		lighting.ClockTime = Clock + 0.001
	end
end

After completing these steps your script should look something like this:

local lighting = game:GetService("Lighting")
local Clock = lighting.ClockTime

local DayilightSpeed = 0.2 
local NightlightSpeed = 0.2

while true do
	Clock = lighting.ClockTime
	if Clock > 6 and Clock < 20.02 then 
		wait(DayilightSpeed)
		lighting.ClockTime = Clock + 0.001
	elseif Clock >= 20.02 or Clock <=6 then
		wait(NightlightSpeed)
		lighting.ClockTime = Clock + 0.001
	end
end

That’s it. It’s very simple and i bet many of you already knew how to do this. So hopefully it helped.

6 Likes

By using this will happen two things: first, the results will not be noticeable. Second, the game will freeze.
So instead of using while true do you could use, for example, RunService.
And you could improve it a lot, since there is some redundant code and some problems (for example the while true do loop) that makes your script not work properly.
Anyway I appreciate the contribution you gave to the community! :smiley:

P.S. When I’ll get to my PC I’ll show you exactly the pieces of code you could modify to make your script work better.

1 Like

While RunService is good, you are incorrect in saying it will freeze.

There is a wait within the loop.

Also,

3 Likes