How to use Tweening for day/night cycle

Use am best:

while true do
    wait()
end

Second, why use a while loop in a spawn unction?

2 Likes

Wow I’m a pleb.

Problem though, having time set to 12 (midday by default) and then tweening to 0 (midnight) tweens down instead of up.

Also, what should I use instead of while wait() do?

Pls read above @NinjoOnline, i already say it

I already answered that before as well

These fire at faster intervals

That makes no difference… if anything it’s just adding an extra line of code, which is a waste

1 Like

For larger game this will make a difference, The While-Wait-Do Idiom, by cntkillme: Addressing while wait() loops

1 Like

Then @NinjoOnline you never defined Lighting, if you not have a variable with this then write game.Lighting, and not simply so Lighting. See above, hope my code will help

ClockTime uses 24 hour formatting if that helps. I personally just use something similar to your code snippet. But I use the return value (the delta time, aka the actual time waited) of wait to make it look more smooth.

local Lighting = game:GetService("Lighting")

local minutes = 0

while true do
    Lighting:SetMinutesAfterMidnight(minutes)
    minutes = minutes + wait(1) -- or whatever time
end
why the while wait() do idiom bad

In your case use

while true do
    -- ...
    wait(n)

It’s clearer.

It clearly indicates that the condition is true and that the loop is infinite because there is no terminating condition.
The while wait() do idiom only works because of a hack; a trick. It abuses the fact that wait returns a number, and numbers are truthy values in Lua. Which has led to tons of bad habits. Such as forgetting about the power of the conditional field of while loop.

while wait(n) do
    if terminating_condition then
        break
        --[[
            Don't get me wrong,
            breaks aren't bad but using them over the conditional part isn't good. That's what it's for.
        ]]--
    end
end

-- vs

while not terminating_condition do
    wait(n)
end

-- So use the latter. It clearly indicates what the condition is.

And you are essentially throwing away that value which would have helped with the second code snippet you provided.

Finally, the idiom is misleading. It implies that the return value of wait is important, when really it isn’t.

There’s also that very unlikely chance that wait might be updated to return false or nil which will break your loops.

That still causes issues with the sun/moon visibly jumping (even if it’s a small jump)

I want a constant, smooth movement.

I already told you that if you want a “smooth movement” then do not use wait() at all.

I’m not…

spawn(function()

	while true do
	    Lighting:SetMinutesAfterMidnight(minutes)
	    minutes = minutes + wait(1)
	end
end)

“I’m not”
Why are you doing wait(1 ) then…???
It will wait for ~1 second to update each time.

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