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)
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)
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)
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).’
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)
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.