How do I make a daynight cycle?

I feel ashamed for not knowing how to do this as a 2 years experience scripter.

local run_service = game:GetService("RunService")
local lighting_service = game:GetService("Lighting")
local daylength = 15 --// Minutes

run_service.Heartbeat:Connect(function()
    --// I can't figure out the math so that a day lasts exactly 15 minutes.
end)
4 Likes
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

local MinutesPerSecond = 1 --1 second equivalent to 1 minute (1 day = 24 minutes).

local function OnHeartbeat(Delta)
	Lighting.ClockTime += (MinutesPerSecond / 60) * Delta
end

RunService.Heartbeat:Connect(OnHeartbeat)
4 Likes

I want it to last 15 minutes, sure, I could change MinutesPerSecond but that would not be as simple as changing the daylength to any number I want. (Also I want to clarify that I suck at math)

1 Like

No one is responding screw this I’ll just use tween service

Here is the code:

local lighting_service = game:GetService("Lighting")
local tween_service    = game:GetService("TweenService")
local daylength        = 15 --// Minutes

local tween = tween_service:Create(lighting_service, TweenInfo.new(daylength * 60), {
	ClockTime = 24
})

tween:Play()

tween.Completed:Connect(function()
	tween:Play()
end)

Do you want a daynight cycle that lasts 15 minutes every day and night?

I want a daynight cycle that makes every day last 15 minutes

So night is 7.5 minutes and day is 7.5 minutes

You basically want each morning and each night to last about 7.5 minutes

That is not a daynight cycle, you manipulating brightness which doesn’t relate to daynight cycle

There are 1440 minutes per day.
If you want a daylength of 15m then each actual min is the same as (1440 / 15).
But you will want your function to run more smoothly than this so we work it out per second (1440 / daylength × 60).
This figure is the step change in minutes after midnight to add each second. But you’ll also need to remember to reset to 0 once it reaches 1440.
Hope that helps with the math.

local run_service = game:GetService("RunService")
local lighting_service = game:GetService("Lighting")
local daylength = 15 --// Minutes
local total_minute = 1440

run_service.Heartbeat:Connect(function()
	local increment = daylength / total_minute
	lighting_service.ClockTime += increment
end)

I don’t understand what you mean by ‘But you will want your function to run more smoothly than this so we work it out per second (1440 / daylength × 60).’

1 Like

Why not using the delta of Heartbeat so the day last exactly the 15 minutes?

local run_service = game:GetService("RunService")
local lighting_service = game:GetService("Lighting")

local Desired = 1 -- 1 minute for testing, change it to 15
local perSec = (24/Desired)/60

run_service.Heartbeat:Connect(function(delta)
	lighting_service.ClockTime += perSec * delta
end)
3 Likes

The step change per minute will make the sun and moon jump across the sky.
Per second looks smoother, as it is many more smaller movements, and @Dev_Peashie suggested using the delta time for even greater precision and even more smaller movements.
The shorter your desired daylength, the more frequently you will want to adjust the lighting.

2 Likes

I read the code and it made so much sense, thank you @Dev_Peashie and @Wigglyaa

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.