How to use Tweening for day/night cycle

I showed a direct answer that does not use the Tween Service because it is even designed to sync time between the client and server and run as smoothly as possible but if you still want to use the tween service for some reason, then go ahead and keep trying to find another solution.

spawn(function()
	while true do
	    Lighting:SetMinutesAfterMidnight(Minutes)
	    Minutes = Minutes + RunService.RenderStepped:wait()
	end
end)

Dont use :BindToRenderStep. This will bind a function to it but all you want to do is wait till the next time it happens. Try the above code.

1 Like

@Dev_HDWC, @NinjoOnline want to tween it

This still results in glitchy performance where the sun/moon will stop for periods, then all of a sudden jump. Iā€™m guessing Iā€™m gonna have to try working it from the client :confused:

Is there a way to do this on client tho effectively :confused:

I could use a remote event, and fire that basically every frame, and have the client update the time of day, but I feel that could cause serious performance issues

Tweens are good because the client actually does the tween too. The glitchiness is probably coming from Lighting replicating slowly, so I suggest you do the tweening on the server for the NumberValue, but then do the SetMinutesAfterMidnight call on the client.

So essentially just moving the Changed event to the client.

1 Like

I mean hereā€™s something that could work out for you.

local Speed = .05

spawn(function()
	while game:GetService("RunService").RenderStepped:wait() do -- Change it to Stepped if inside a Normal Script
		Lighting:SetMinutesAfterMidnight(Lighting:GetMinutesAfterMidnight() + Speed)
	end
end)

That wonā€™t run at a consistent speed. Youā€™re better off using event-based programming to account for different time steps.

Would i have to then parent the NumberValue to RepStorage then?

you made the tween but you never played it, you would have to do

ToMidnight:Play()
ToMidnight.Completed:Wait()

It will replicate fine in Lighting, so itā€™s up to you. Hereā€™s edited and tested code based on the previous snippets I gave split into server and client. Seems to work fine for me:

Script in ServerScriptService:

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

local number = Instance.new( 'NumberValue', Lighting )
number.Name = 'MinutesAfterMidnight'
local tweenInfo = TweenInfo.new( 60 * 10, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, -1 )

-- Create the tween
local dayCycle = TweenService:Create( number, tweenInfo, { Value = 60 * 24 } )

-- Initialise
number.Value = 0
dayCycle:Play()

LocalScript in ReplicatedFirst:

local Lighting = game:GetService( 'Lighting' )
local number = Lighting:WaitForChild( 'MinutesAfterMidnight' )

-- Set up the event
number.Changed:Connect( function( value )
    Lighting:SetMinutesAfterMidnight( value )
end )
If you want the day to start at midday when the server first loads:

Script in ServerScriptService:

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

local number = Instance.new( 'NumberValue', Lighting )
number.Name = 'MinutesAfterMidnight'
local tweenInfoIn = TweenInfo.new( 60 * 5, Enum.EasingStyle.Quad, Enum.EasingDirection.In )
local tweenInfoOut = TweenInfo.new( 60 * 5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out )

-- Create the tweens
local toMidnight = TweenService:Create( number, tweenInfoOut, { Value = 60 * 24 } )
local fromMidnight = TweenService:Create( number, tweenInfoIn, { Value = 60 * 12 } )

-- Set up the events
toMidnight.Completed:Connect( function()
    number.Value = 0
    fromMidnight:Play()
end )
fromMidnight.Completed:Connect( function()
    number.Value = 60 * 12
    toMidnight:Play()
end )

-- Initialise
number.Value = 60 * 12
toMidnight:Play()

LocalScript is exactly the same as before.

The reason for the client one being in ReplicatedFirst is to minimise the time before the clock changes (so you donā€™t suddenly see the game flick from midday to night time if the server is currently in the middle of the night).

There is very minor glitchiness in the shadows. Itā€™s only ShadowMap shadows that glitch slightly - the regular shadows in Voxel mode are perfect. The sun and moon move smoothly so I think this is due to the ShadowMap technology, and even tweening on the client does not completely eradicate this. Using TweenService is the best itā€™s going to get.

Worth noting these snippets use Quad stlye. If you want an equal time step throughout then swap the EasingStyle to Linear - I only kept it as Quad to match your original post.

12 Likes