How to make nights shorter with the Day/Night cycle

I used this simple script to make the day/night cycle:

local lighting = game.Lighting

while true do 
    lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + 0.75)
     wait(1)
end

The problem is that I want the nights to be shorter. I made my own messy code which I was unsatisfied with:

local lighting = game.Lighting

lighting.TimeOfDay = "15:00:00"
wait(1)
lighting.TimeOfDay = "16:00:00"
wait(1)
Etc... 

Ugly I know!
Once the time of day reached 19 I skipped to 21 after a few second and then skipped to 5. Then I switched around the time of the waits so it would be a slower transition. The problem was that the time transition was super abrupt.

So I came to the forum:
Is there a way I can make the nights shorter?

PS I'm using shadow map
1 Like

Well, I’ll try to write for your attempt.

You can say:

local lighting = game:GetService("Lighting")

local information = {
    Time = 1; -- The time between each increment
    Increment = .5 -- the amount that the time of day will increment by
}

while true do -- repeat while the server is open
    lighting.ClockTime = lighting.ClockTime + information.Increment -- increase the time by (in this case) 1/2 of a second
    wait(information.Increment) -- wait the set time
end

Hope this helped!

EDIT: This would be a more suitable solution, actually.

Also make sure to use the RunService to keep the movement of the cycle smooth (Wait()) makes it look choppy.
You could also just adjust the value to whatever seems to fit what you need.

local lighting = game.Lighting
local MAM = 0
local Time = 1 -- set to anything

while true do
        lighting:SetMinutesAfterMidnight(MAM)
       MAM = MAM + Time
     game:GetService("RunService").Heartbeat:Wait()
end

Yep, this is actually a good way to do it. It’s really personal preference, since if you make the time quick enough it’s not choppy. If you are looking to have it slower than most games, this is the way to go. :slight_smile:

Both your and @Sqowly’s codes work. The problem is that it makes the day run as fast as the night time. The reason I made that code in the above post was so that it would stay at a normal pace for the day but go faster for the night-time.

I now used ClockTime instead since its easier to use. It’s choppy but it’s more effective than my previous code:

local lighting = game.Lighting

lighting.ClockTime = 15
wait(2)
lighting.ClockTime = 15.5
wait(2)
lighting.ClockTime = 16
wait(2)
lighting.ClockTime = 16.5
wait(2)
lighting.ClockTime = 17
wait(2)
lighting.ClockTime = 17.5
wait(2)
lighting.ClockTime = 18
wait(2)
lighting.ClockTime = 18.5
wait(2)
lighting.ClockTime = 19
wait(2)
lighting.ClockTime = 21
wait(5)
lighting.ClockTime = 24
wait(5)
lighting.ClockTime = 4
wait(1)
lighting.ClockTime = 4.5
wait(1)
lighting.ClockTime = 5
wait(1)
lighting.ClockTime = 5.5
wait(1)
lighting.ClockTime = 6
wait(1)
lighting.ClockTime = 6.5
wait(1)
lighting.ClockTime = 7
wait(1)
lighting.ClockTime = 7.5
wait(1)
lighting.ClockTime = 8
wait(1)
lighting.ClockTime = 8.5
wait(1)
lighting.ClockTime = 9
wait(1)

But this is a bit exhausting code. Not very clear and concise.
To make the night shorter I skipped from 19 to 21 then 21 to 24 and 24 to 4. Then I went in normal increments of .5 for the rest of it.

It’s still not pretty

Although the waits are extremely excessive, atleast you got what you needed

1 Like

This will also not run forever, since it’s not in a loop
you can either use a:

While
or a
for Loop allow this to go on “Forever”

Ah, thank you for pointing that out!

@Sqowly Works and isn’t what the OP wanted.

I would also not recommend supporting that at all either.

What you could do instead is a combination of a loop and ifstatement.

Using your code from before and changing it:

 local lighting = game:GetService("Lighting") -- Use getservice instead

while true do 
    if if lighting.TimeOfDay >= bla bla and lighting.TimeOfDay <= -- night (probs a better way but this is something that is much shorter)
lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + 0.45) 
     wait(1)
elseif lighting.TimeOfDay >= bla bla and lighting.TimeOfDay <= then -- day (probs a better way but this is something that is much shorter)
    lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + 0.75) 
     wait(1)
end

-- this is only an example, you'll want to change the if statement values and change the day value so that if fits your script.

Code smells are generally often terrible practice and shouldn’t be done (as you’ve done with the huge amount of waits) unless it’s your last resort and even then, there is still various amount of ways to have a better way of scripting it.

Maybe use the “RunService” to keep things smooth

Thank you for pointing that out. And yes, I know the waits are excessive.

Tweening would also work in this case if done on the client to make things more smooth but that’ll require some remote events to keep in sync time.

Though :+1:

Thank you, your code is much more effective than mine :smirk:. Although at the very end of the code the output sends an error message saying “Script timeout: exhausted allowed execution time”.