How would I approach a day and night cycle?

I’m a little trapped on how to make a specific day and night cycle. I know that you can loop SetMinutesAfterMidnight on Lighting, but I’m looking to make a more advance system where, say, a day lasts for 10 minutes and a night for 5 minutes. I believe it’ll have to accommodate for foreign changes as well (if day and console sets ClockTime to night, adjust cycle).

I can’t even so much as make a system to make a day last for a certain time, half taking day and half taking night (15-minute full days).

4 Likes

So far, I’ve chalked this up (free code, yay!). I don’t have control over how many minutes day and night are, or how long the whole day altogether is, but it does something that’s good enough for now.

local Lighting = game:GetService("Lighting")

repeat
	local ProgressDelay = 3
	if Lighting.ClockTime < 6 and Lighting.ClockTime > 18 then
		ProgressDelay = ProgressDelay/2
	end
	Lighting.ClockTime = Lighting.ClockTime + 0.02
	wait(ProgressDelay)
until false
3 Likes

My good ole goto would usually be…

local Lighting = game:GetService('Lighting')

while true do
    local Sec = wait(1);

    Lighting:SetMinutesAfterMidnight(Lighting:GetMinutesAfterMidnight() + Sec);
end;

You can tinker with that to change lengths and such, but essentially, it makes use of the Set and Get minutes after midnight functions that Roblox provides, and adds the delay ran in wait in the form of minutes, with the wait used to make sure lag doesn’t interfere too much with the cycle.

1 Like

Hmm… I can give you something which should help out.

local DAY_LENGTH=10; --Length of the day in minutes.
local NIGHT_LENGTH=5; --Length of the night in minutes.

local startTime = tick();
while wait(1) do --The wait here is responsible for determining how smooth the transition is.
    --The current time in minutes since daybreak (where we start our cycle).
    local currentTime = (tick() - startTime) / 60 % (DAY_LENGTH + NIGHT_LENGTH);

    if currentTime < DAY_LENGTH then
        --Fits the clock time between 6 and 18 when currentTime is between 0 and DAY_LENGTH
        Lighting.ClockTime = 6 + 12 * currentTime / DAY_LENGTH
    else
        --Fit the clock time between 18 & 30 based on where currentTime is between DAY_LENGTH and NIGHT_LENGTH+DAY_LENGTH. E.g., if DAY_LENGTH=5 and NIGHT_LENGTH=10, currentTime = 10 would be halfway between the two; halfway between 18 & 30 would give 24.
        --If we exceed 24, wrap over to 0.
        Lighting.ClockTime = (18 + 12 * (currentTime - DAY_LENGTH) / NIGHT_LENGTH) % 24;
    end
end

This should give you the different night/day lengths that you’re asking for. Quick disclaimer: I did not test.

24 Likes