Day Night Cycle [ Smooth ]

Day & Night Cycle - Preview


INFORMATION

  • Lighting tweens locally

  • TweenService used for maximum smoothness & diverse styles

  • Configurable : EasingStyle, EasingDirection, time_Interval, time_Increment

  • Global Time - everyone shares the same time regardless of when they join

  • TweenInfo changable in run time

Limitations ( Network & Device )
  • TweenService tweens from any higher number than 0 to 0, reversing time, thus time will be set to 0 if the Tween fails to reach 24.

  • Replication from Server to Client is not real time. To combat this, prior tweens created will pause and be dismissed, playing the newer one.

Documentation


Module_Time:start_Time()

Documentation : start_Time

Information

  • Starts Day Night Cycle.

Example

local Module_Time	=	require(script:WaitForChild("Module_Time")) -- Location of "Module_Time"

Module_Time:start_Time()

Module_Time.time_Wait ( REPLICATES )

Documentation : time_Wait

Information

  • Determines the time the loop pauses before running again.

  • Expects a number.

Example

Module_Time.time_Wait = .25 --Now the loop waits .25 seconds before updating time.

Module_Time.time_Increment ( REPLICATES )

Documentation : time_Increment

Information

  • Determines the hours advanced per ran loop.
  • Expects a number.

Example

Module_Time.time_Increment = 1 --Now the time increments by 1 hour every time the loop runs

Module_Time.tween_EasingStyle ( REPLICATES )

Documentation : tween_EasingStyle

Information

  • Determines the time the EasingStyle used in the tween.

  • Expects a string (name of the easing style)

Example

Module_Time.tween_EasingStyle = "Linear" --Replicates, Clients will now use style.

Module_Time.tween_EasingDirection ( REPLICATES )

Documentation : tween_EasingDirection

Information

  • Determines the time the EasingDirection used in the tween.

  • Expects a string (name of the easing direction)

Example

Module_Time.tween_EasingDirection = "In" --Replicates, Clients will now use direction.

Love it? Use it.


Local Script OR Script with RunContext as Client
local tween_Current	=	nil

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

local tween_Info			=	TweenInfo.new(Service_Lighing:GetAttribute("time_Wait"),Enum.EasingStyle[Service_Lighing:GetAttribute("tween_EasingStyle")],Enum.EasingDirection[Service_Lighing:GetAttribute("tween_EasingDirection")],0,false,0)

Service_Lighing.ClockTime	=	Service_Lighing:GetAttribute("Time")


Service_Lighing:GetAttributeChangedSignal("Time"):Connect(function()

	if tween_Current and tween_Current.PlaybackState == Enum.PlaybackState.Playing then
		
		tween_Current:Pause()
		
		--print("Paused Prior Track")
		
	end
		
	if Service_Lighing.ClockTime > Service_Lighing:GetAttribute("Time") then
		
		Service_Lighing.ClockTime	=	0
		
--		print("Patched : Time = 0")
		
	else
		
		tween_Current	=	Service_TweenService:Create(Service_Lighing,tween_Info,{ClockTime = Service_Lighing:GetAttribute("Time")})		
		
		tween_Current:Play()
		
	end
	
	--print(Service_Lighing:GetAttribute("Time"))
	
end)



Service_Lighing:GetAttributeChangedSignal("time_Wait") do

	tween_Info	=	TweenInfo.new(Service_Lighing:GetAttribute("time_Wait"),tween_Info.EasingStyle,tween_Info.EasingDirection,0,false,0)

end


Service_Lighing:GetAttributeChangedSignal("tween_EasingStyle") do

	tween_Info	=	TweenInfo.new(tween_Info.Time,Enum.EasingStyle[Service_Lighing:GetAttribute("tween_EasingStyle")],tween_Info.EasingDirection,0,false,0)

end


Service_Lighing:GetAttributeChangedSignal("tween_EasingDirection") do
	
	tween_Info	=	TweenInfo.new(tween_Info.Time,tween_Info.EasingStyle,Enum.EasingDirection[Service_Lighing:GetAttribute("tween_EasingDirection")],0,false,0)
	
end
Module Script
return {

	time_Wait				=	.25,
	time_Increment			=	1,
	tween_EasingStyle		=	"Linear",
	tween_EasingDirection	=	"Out",
	
	start_Time	=	function(self)
		
		if not game.Lighting:GetAttribute("Time") then game.Lighting:SetAttribute("Time",0) end
		if not game.Lighting:GetAttribute("time_Wait") then game.Lighting:SetAttribute("time_Wait",self.time_Wait) end
		if not game.Lighting:GetAttribute("time_Increment") then game.Lighting:SetAttribute("time_Increment",self.time_Increment) end
		if not game.Lighting:GetAttribute("tween_EasingStyle") then game.Lighting:SetAttribute("tween_EasingStyle",self.tween_EasingStyle) end
		if not game.Lighting:GetAttribute("tween_EasingDirection") then game.Lighting:SetAttribute("tween_EasingDirection",self.tween_EasingDirection) end
		
		
		while task.wait(self.time_Wait) do
			
			if game.Lighting:GetAttribute("Time") + self.time_Increment <= 24 then

				game.Lighting:SetAttribute("Time",game.Lighting:GetAttribute("Time") + self.time_Increment)				
				
			else

				game.Lighting:SetAttribute("Time",self.time_Increment)
				
			end

		end

	end,
	
	
}
Script with RunContext Legacy OR Server
local Module_Time	=	require(script:WaitForChild("Module_Time"))

Module_Time:start_Time()

Suggestions and Feature Requests are always open.

31 Likes

how about being able to choose how long day and night are individually

I’ve tried doing this myself but I couldn’t Thank you!

You can, it’s on the time_Increment and time_Wait documentations.

Simply do

Module_Time.time_Increment = 1 -- 1 hour per loop
1 Like

i mean as in the ability to make the day last longer than the night

Thank you, if you have any suggestions or feature requests, feel free to request them.

I created this as my game required it. This is the less complex and beta version.

Ah, I could fork it for you. Do tell me what you consider night ( example : after 8 PM to 0 AM)

1 Like

i consider night from 9:30pm til 6am

0.5 would be 30 mins and .25 = 15 mins etc?

It uses ClockTime to skip extensive calculations and formatting so it may be off by a little if you use low decimal values. It’ll still work.

I’ll have one out for you or add it in an eventual second version as it’d have additional conditions and behaviour. If I find time I’ll either post it here as a forked version or private message you it.

1 Like

thank you!!

1 Like

What would be the math behind it then?

can time be pauses and played locally, dont want to force everyone to have the cycle, but have it as an option.

Wouldn’t creating excessive amount of tween can cause memory leak? Don’t get me wrong, I like this, but it can be optimized way more.

How about a stop time feature? -----------

The GC should be collecting them once they’re overridden, though I could add in a Tween:Destroy().

To sum up the post below from a fellow devforum member: Tweens, like any other Instances, will be garbage collected when no longer pointed to.

I was thinking about this as well, sure I’ll add it in.

Could you describe it further @Wertyhappy27? If you mean enable/disable the script at will, you could do that with a TextButton.

TextButton.Activated:Connect(function()

LocalScript.Enabled = not LocalScript.Enabled

end

The maths? We got a mathematician! I personally tried to make this lean away from extensive maths so

  • A) Less changes have to be Replicated

  • B) User friendly

But I’ll probably shift to TimeOfDay.

Maths behind it:
Simply find out what is the % of minutes to an hour.

Divide the number of minutes by 60 , which is the number of minutes in an hour. For example, convert 14.75 minutes to a percentage by dividing 14.75 by 60, which equals about 0.246, or 24.6 percent." - Google Search

Or alternatively search up “what is 70% of one hour” online. Input answer to attain the corresponding time.

Just to be clear, if a player joined their time and tween would automatically be synced, correct?