Day - Night Cycle working very weirdly

Here is the script I’m using for Day-Night cycle.

local Lighting = game.Lighting
local TweenService = game:GetService( 'TweenService' )


local tweenInfoIn = TweenInfo.new( 12, Enum.EasingStyle.Quad, Enum.EasingDirection.In )
local tweenInfoOut = TweenInfo.new( 12, Enum.EasingStyle.Quad, Enum.EasingDirection.Out )

-- Create the tweens
local toMidnight = TweenService:Create( Lighting, tweenInfoOut, { ClockTime = 24 } )
local fromMidnight = TweenService:Create( Lighting, tweenInfoIn, { ClockTime = 12 } )

-- Set up the events
toMidnight.Completed:Connect( function()
	Lighting.ClockTime = 0
	print("Midnight")
	fromMidnight:Play()
end )
fromMidnight.Completed:Connect( function()
	Lighting.ClockTime = 12
	toMidnight:Play()
end )


toMidnight:Play()

And here is how it behaves

Issues, that I don’t understand:

  1. The sun moves much faster than the moon, even tho for both tweens there is the same increment to move, and same time for tweening.
  2. When the moon reaches top, it stops, does a weird rotation, and only then continues to move.
  3. I’m using ClockTime = 24 for midnight, and ClockTime = 12 for day. Ideally, I would like to instead of this use ClockTime = 18 for midnight, and ClockTime = 7 for day, because those are the times that it actually gets dark and light. The reason I’m not using it now, is because when I tween from 18 to 7, it naturally goes backwards, instead of tweening first to 24, and then to 7, which I don’t know how to do in an optimized way.

I would much appreciate if someone would help me understand and fix this issues :smiley:

Why not just tween from 0 to 24? Instead of having 2 separate states?

Because I need this to be a permanent thing, and if I tween from 0 to 24 then reverse it, the sun will start moving backwards. + I need to do some things in the game when it’s midnight, for now I just printed midnight, but there will be other code

Oh in that case I would recommend setting the EasingStyle to linear to get the same speed, or changing the EasingDirection so they are both the same.

1 Like
for i=1,math.huge do
   game.Lighting:SetMinutesAfterMidnight(i)
end

https://developer.roblox.com/en-us/api-reference/function/Lighting/SetMinutesAfterMidnight

Issue 1 is solved now. Thank You!
I realised the reason for Issue 2, but it’s unimportant.

Do you perhaps know how I could achieve what I want in Issue 3?

fix issue 2 using my post

I have a couple of ways to achieve this, I made a day/light cycle. I haven’t tested it but it should work. Feel free to use or get ideas. I added some settings at the top which you can mess around with that until you like it. This is probably the best the thing I can come up with and it should be fairly optimised.

--@@ Author: TheDCraft
--@@ Title: Day/Night Cycle

--// Settings
local DEBUG_MODE = true -- If true will print whether its day or night
local TIME_FOR_CYCLE: number = 10
local EASING_STYLE, EASING_DIRECTION = Enum.EasingStyle.Linear, Enum.EasingDirection.InOut
local LOOP: boolean = true -- If false it will only run once
local DAY_START, DAY_END = 7, 17 -- ClockTime Number

--// Services
local TweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")

--// TweenInfo
local Tweeninfo = TweenInfo.new(TIME_FOR_CYCLE, EASING_STYLE, EASING_DIRECTION, LOOP and -1 or 0)

--// Ligting
Lighting.ClockTime = 0

--// Variables
local isDay = false
local isNight = false

--// Tween
TweenService:Create(Lighting, Tweeninfo, {ClockTime = 24}):Play()

--// New Thread
task.spawn(function()
	while true do
		task.wait(0.5)
		local Time = math.round(Lighting.ClockTime)
		if Time >= DAY_START and Time <= DAY_END then
			isNight = false
			isDay = true
		else
			isDay = false
			isNight = true
		end
		if DEBUG_MODE then
			print(isDay and "Day" or "Night")
		end
	end
end)

--// Functions
--@@ Returns whether its day or night
local function GetTime(): boolean
	return isDay and "Day" or "Night"
end

I think issue 2 has been solved, it was due to the tween I believe