Day / night cycle help

Here’s my day / night cycle I wrote:

while true do
	-- Countdown
	if secondsRemaining.Value <= 0 then
		day = not day -- switch
		
		if not day then
			-- its night
			secondsRemaining.Value = totalNightDuration
			
			local tweenInfo = TweenInfo.new(secondsRemaining.Value, Enum.EasingStyle.Linear)
			local tween = TweenService:Create(Lighting, tweenInfo, {ClockTime = 7})
			
			tween:Play()
			
			print("playing to become day")
		else
			-- its now day
			secondsRemaining.Value = totalDayDuration
			
			local tweenInfo = TweenInfo.new(secondsRemaining.Value, Enum.EasingStyle.Linear)
			local tween = TweenService:Create(Lighting, tweenInfo, {ClockTime = 19})

			tween:Play()
			
			print("playing to become night")
		end
	end
	
	secondsRemaining.Value -= 1
	
	print(secondsRemaining.Value)
	task.wait(1)
end

there’s a problem tho, since after the night phase starts the tween goes from 19 to 7 by subtracting, not going 19 - 24 - 7 as I want. Do you have an idea on how to fix this?

1 Like

You could tween a number value instead of the ClockTime directly, and set ClockTime to the value mod 24 every frame.

game.Lighting.ClockTime = workspace.Time.Value % 24

NOTE: There may be a better way of doing this, but this is my method.

doesn’t work, same thing happens.

Did you tween the number value instead? That bit of code was to update the ClockTime based on what the value was. If it went over 24, it would minus out 24 from the clock time. For example: 25 % 24 would be 1.

while true do
	-- Countdown
	if secondsRemaining.Value <= 0 then
		day = not day -- switch

		if not day then
			-- its night
			secondsRemaining.Value = totalNightDuration

			local tweenInfo = TweenInfo.new(secondsRemaining.Value/2.4, Enum.EasingStyle.Linear)
			local tween = TweenService:Create(Lighting, tweenInfo, {ClockTime = 24})
			
			tween.Completed:Once(function()
				local tweenInfo = TweenInfo.new(secondsRemaining.Value, Enum.EasingStyle.Linear)
				local tween = TweenService:Create(Lighting, tweenInfo, {ClockTime = 7})
				tween:Play()
			end)

			tween:Play()

			print("playing to become day")
		else
			-- its now day
			secondsRemaining.Value = totalDayDuration

			local tweenInfo = TweenInfo.new(secondsRemaining.Value, Enum.EasingStyle.Linear)
			local tween = TweenService:Create(Lighting, tweenInfo, {ClockTime = 19})

			tween:Play()

			print("playing to become night")
		end
	end

	secondsRemaining.Value -= 1

	print(secondsRemaining.Value)
	task.wait(1)
end

try this
EDIT: for explanation - TweenService does not know that some values can circle back and forth, going from 24 to 0. The only way it found is to go backwards, which is exactly what happened here.

I’d just set the time to 0 to begin with, tween it to 24, then restart it.

timeS = 720 --How long a day is, in seconds.

function tweenTime()
    game.Lighting.ClockTime = 0
    local tween = game:GetService("TweenService"):Create(game.Lighting,TweenInfo.new(timeS,Enum.EasingStyle.Linear),{ClockTime = 24})
    tween:Play()
    tween.Completed:Wait() --I forgot the event name, but I think it's "Completed"
    tweenTime()

tweenTime()

How did you get this number from? / 2.4

the script you have provided me actually works, but only if the day lasts the same time the night does. I have two variables called

local dayDurationInMinutes = 1  -- Duration of the day phase in real-world minutes
local nightDurationInMinutes = 1  -- Duration of the night phase in real-world minutes

Cause if change these to a diff value the tween from 19 to 24 moves the moon faster than the one from 24 to 7. Thanks!

actually. Im almost sure I tried your method.

I tried my method and it worked out just fine. When te number does go to 31, (aka 7,) set it back to 7 just before going back to 19. You could set the value initially with the same equation before the next time change.

workspace.Time.Value %= 24

you don’t even need to wait for its completion, as a tween can have a repeat value of -1 to loop forever or until stopped

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local timeS = 720 --How long a day is, in seconds.
local TI = TweenInfo.new(timeS, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1)

Lighting.ClockTime = 0
local Tween = TweenService:Create(Lighting, TI, {ClockTime = 24})
Tween:Play()