Ambience not changing according to day/night cycle

Hey, so I’m here to ask this for a friend, he’s having a bit of an issue with a script and came to me so that I can ask you guys for support, he says:

I’m currently trying to make a script where the ambience changes during the day and night, and I’ve made this so far.
The ambience does not change however, and I cannot see the issue in the script. There are no output errors.

local NightTime = 18
local MorningTime = 6
local Lighting = game.Lighting
local Time = Lighting:GetMinutesAfterMidnight()

if Time > NightTime and Time < MorningTime then
     Lighting.Ambient = Color3.fromRGB(0, 0, 0)
else
    Lighting.Ambient = Color3.fromRGB(158, 162, 119)
end
1 Like

You’d want to multiply the NightTime and MorningTime by 60 to get the minutes from the hours you provided, because the function is called GetMinutesAfterMidnight:

local NightTime = 18 * 60
local MorningTime = 6 * 60

Alright, thanks a bunch, I’ll make sure to forward the reply to him.

It is also not being looped make sure you are looping the if then statement so it updates.

He tried this and it came up with an output error saying,

-Script timeout: exhausted allowed execution time

Make sure that there is some sort of yield in the while loop, or else the script will just time-out. Adding a wait in the loop is the best option:

local NightTime = 18
local MorningTime = 6
local Lighting = game.Lighting
local Time = Lighting:GetMinutesAfterMidnight()

while true do
    if Time > NightTime and Time < MorningTime then
        Lighting.Ambient = Color3.fromRGB(0, 0, 0)
    else
        Lighting.Ambient = Color3.fromRGB(158, 162, 119)
    end
    wait(1)
end

Sorry for bothering but, the ambience still hasn’t changed for him. This is the script he’s using:

local NightTime = 18 * 60
local MorningTime = 6 * 60
local Lighting = game.Lighting
local Time = Lighting:GetMinutesAfterMidnight()

while true do
if Time > NightTime and Time < MorningTime then
     Lighting.Ambient = Color3.fromRGB(0, 0, 0)
else
    Lighting.Ambient = Color3.fromRGB(158, 162, 119)
    end
    wait(1)
end

Right, I forgot – the Time variable in the script is kept constant, so you’d need to get the current value in the loop itself:

local NightTime = 18
local MorningTime = 6
local Lighting = game.Lighting

while true do
    local Time = Lighting:GetMinutesAfterMidnight()
    if Time > NightTime and Time < MorningTime then
        Lighting.Ambient = Color3.fromRGB(0, 0, 0)
    else
        Lighting.Ambient = Color3.fromRGB(158, 162, 119)
    end
    wait(1)
end

Why didn’t I realise that sooner.

It’s better To use GetPropertyChangedSignal() instead of putting it inside a loop

Do you mean to use ClockTime?

local NightTime = 18
local MorningTime = 6
local Lighting = game.Lighting
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if Time < NightTime and Time > MorningTime then --  I also reversed the > and <
         Lighting.Ambient = Color3.fromRGB(0, 0, 0)
    else
        Lighting.Ambient = Color3.fromRGB(158, 162, 119)
    end
end)

Clock time returns the hour in 24 hours, while :GetMinutesAfterMidnight returns the minutes after midnight