How to use Tweening for day/night cycle

Cuase that’s what @incapaxx did… I got several people giving me several different solutions

Yes but clearly, anything involving wait() will wait for at least 0.033 seconds (30 fps) which is a lot more than 0.0166- (60+ fps). Try taking a look at BindToRenderStep from the RunService.

spawn(function()
	while true do
	    Lighting:SetMinutesAfterMidnight(Minutes)
	    Minutes = Minutes + RunService:BindToRenderStep()
	end
end)

Argument missing on the BindToRenderStep() line.

DevHub is down, so I can’t look into what any parameters it passes through

It is working for me. This is one of their example code blocks

-- Make variables for Roblox services
local RunService = game:GetService("RunService")
 
-- Function that will be bound to the render step
local function checkDelta(deltaTime)
	-- Print the time since the last render step
	print("Time since last render step:", deltaTime)
end
 
-- Bind the function
RunService:BindToRenderStep("Check delta", Enum.RenderPriority.First.Value, checkDelta)

The name is just there so you can UnBind later, and the priority is just when it should execute relative to things like the camera or character that also happen every frame.

You would still want to do this locally though and sync the time from the server in one of the ways I have mentioned previously. Otherwise, there will be lag from you not having google fiber levels of connection to roblox servers.

If you are going for just the basic day/night cycle. You can use this script, which also cycles based on the time (in seconds) you give.

local TweenService = game:GetService("TweenService")
local CycleTime = 360 -- Seconds for a full day to cycle
local LightGoal = {}
LightGoal.ClockTime = 24
local LightStyle = TweenInfo.new(CycleTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
TweenService:Create(game.Lighting, LightStyle, LightGoal):Play()
delay(0, function()
    game.Lighting.ClockTime = 0 -- begins at any given ClockTime
    local LightGoal = {}
    LightGoal.ClockTime = 24 -- ends at given ClockTime, cycles to the beginning day 
    local LightStyle = TweenInfo.new(CycleTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1)
    TweenService:Create(game.Lighting, LightStyle, LightGoal):Play()
end)
4 Likes

Why you still use while loops into a spawn funcion? Then, does the script snippets that incapaxx, Wunder_Wulfe or i work? If no, then say it, i actually not understand what now your problem is, you use TweenService and i was done. Read the whole chat from above and then say us what really your problem is, i really not understand now…

What delay make? And the CycleTime would be 1440, as 24*60 = 1440. Am curious of what it make…

1 Like

It was just for customized times. If he wanted to make it so that a full day would happen every X seconds, he could replace the time with it.

1 Like

How does this have anything to do with my code…

local DayCycle = {}

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

local Minutes = 0

spawn(function()
	while true do
	    Lighting:SetMinutesAfterMidnight(Minutes)
	    Minutes = Minutes + RunService:BindToRenderStep() -- ERROR
	end
end)

return DayCycle

Argument 2 missing or nil

1 Like

What‘s line @NinjoOnline? Without we can‘t help. And why you still are a while loop in a spawn function?

Edit: See the doc of Roblox

I put a comment in the script saying where the error is

1 Like

RunServiceBindToRenderStep.pdf (1,3 MB)

How can I use it in my code tho… nobody is yet to give code which works

Try to avoid loops that never end. Use event-based programming instead.

If you don’t mind it starting at midnight you can simply make a looping tween on a NumberValue and then use the SetMinutesAfterMidnight method on Lighting.

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

local number = Instance.new( 'NumberValue', Lighting )
number.Name = 'TweenValue'
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 } )

-- Set up the event
number.Changed:Connect( function( value )
    Lighting:SetMinutesAfterMidnight( value )
end )

-- Initialise
number.Value = 0
dayCycle:Play()
If it absolutely must start at midday, then it gets slightly more complex, where we trigger one tween as the other completes.
local Lighting = game:GetService( 'Lighting' )
local TweenService = game:GetService( 'TweenService' )

local number = Instance.new( 'NumberValue', Lighting )
number.Name = 'TweenValue'
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
number.Changed:Connect( function( value )
    Lighting:SetMinutesAfterMidnight( value )
end )
toMidnight.Completed:Connect( function()
    Lighting:SetMinutesAfterMidnight( 0 )
    fromMidnight:Play()
end )
fromMidnight.Completed:Connect( function()
    Lighting:SetMinutesAfterMidnight( 60 * 12 )
    toMidnight:Play()
end )

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

Tweening a NumberValue with no parent gives you greater control than simply using ClockTime which I try to avoid at all costs. The same technique can be applied to anything else. Methods cannot be directly tweened, but their inputs can be using this procedure.

2 Likes

Usually I was using just this to loop the day and night

while true do
    wait(--[[some time]])
    game.Lightning.ClockTime = game.Lightning.ClockTime + --some value
end

and in one of my games I used an “if” statement to check if the time is night to speed it up because everyone hates nights!

Hey, we’re trying to help you find your method, we’re not doing all the work for you. You should know that if you read the guidelines in the scriptingsupport category.

1 Like

I repeat it, you never defined what Lighting is (here is no Variables that has as name Lighting, if this was so then write game.Lighting)

Once that ends tho it won’t repeat tho… I need it to repeat indefinetely.

… Wowwee :roll_eyes:

I gave the code sample in my question. I’m not saying ‘make this for me’ I’m saying what’s wrong with this, how can I fix it. Most answers have avoided actually showing a direct answer. No point describing a solution without actually having the solution… basically avoiding the answer

1 Like

Yes it will repeat.

If you’re on about the first code snippet, the repeating value is set to minus 1 in the tween info. Check the docs for more info about all the properties.

The last example on this page explains the behaviour a bit further if you get stuck or want to learn more: TweenService | Documentation - Roblox Creator Hub