I have a lightingManager script that’s supposed to lerp between different ambient based on the time in the game. It works fine until it gets to midnight. Then the whole script breaks, it keeps running but the interpolation factor becomes 1 so the lighting stops changing.
local Lighting = game:GetService("Lighting")
local Terrain = workspace:FindFirstChild("Terrain")
-- Define settings for different times of day
local daySettings = {
{
Time = 6, -- Morning starts at 6 AM
ColorShift_Top = Color3.new(1, 0.894, 0.843), -- Warm orange
ColorShift_Bottom = Color3.new(0.6, 0.553, 0.506), -- Warm brown
OutdoorAmbient = Color3.new(1, 1, 1), -- White
Ambient = Color3.fromRGB(255, 247, 233), -- Light warm color
CloudColor = Color3.fromRGB(255, 200, 150) -- Orangish sunrise
},
{
Time = 12, -- Noon starts at 12 PM
ColorShift_Top = Color3.new(1, 1, 1), -- Bright white
ColorShift_Bottom = Color3.new(0.8, 0.8, 0.8), -- Neutral gray
OutdoorAmbient = Color3.new(1, 1, 1), -- White
Ambient = Color3.fromRGB(255, 247, 233), -- Light warm color
CloudColor = Color3.new(1, 1, 1) -- White
},
{
Time = 18, -- Evening starts at 6 PM
ColorShift_Top = Color3.new(1, 0.898, 0.882), -- Reddish hue
ColorShift_Bottom = Color3.new(0.4, 0.353, 0.376), -- Cool dark
OutdoorAmbient = Color3.new(0.5, 0.5, 0.5), -- Dimming
Ambient = Color3.fromRGB(180, 170, 160), -- Transitioning
CloudColor = Color3.fromRGB(255, 180, 120) -- Orangish sunset
},
{
Time = 21, -- Night starts at 9 PM
ColorShift_Top = Color3.new(0.4, 0.4, 0.5), -- Dark blue
ColorShift_Bottom = Color3.new(0.2, 0.2, 0.3), -- Dark
OutdoorAmbient = Color3.new(0.2, 0.2, 0.3), -- Dim but not full black
Ambient = Color3.fromRGB(100, 95, 90), -- Dark gray
CloudColor = Color3.fromRGB(70, 70, 80) -- Dark clouds
}
}
-- Function to clamp values between 0 and 1
local function clamp(value, min, max)
return math.max(min, math.min(max, value))
end
-- Function to interpolate between two values
local function lerp(a, b, t)
return clamp(a + (b - a) * t, 0, 1)
end
-- Function to interpolate between two Color3 values
local function lerpColor3(a, b, t)
return Color3.new(
lerp(a.R, b.R, t),
lerp(a.G, b.G, t),
lerp(a.B, b.B, t)
)
end
--------------------------------------------------------------------------
local currentTime = 23.7 * 60 -- Starts at 11 PM
local gameMinutesPerSecond = 1 -- 1 second in runtime = 1 in-game minute
local updateInterval = 1 -- Update every second
while true do
-- Increment time and ensure it wraps around after 1440 minutes (24 hours)
currentTime = (currentTime + gameMinutesPerSecond) % 1440
local currentHour = (currentTime / 60) % 24 -- Convert minutes to hours and wrap to 24-hour format
-- Find the current and next settings based on the time of day
local currentSettings, nextSettings, t
for i = 1, #daySettings do
if currentHour < daySettings[i].Time then
currentSettings = daySettings[i == 1 and #daySettings or i - 1]
nextSettings = daySettings[i]
t = (currentHour - currentSettings.Time) / (nextSettings.Time - currentSettings.Time)
break
end
end
-- Handle transitions around midnight (when the time rolls over)
if not currentSettings then
currentSettings = daySettings[#daySettings]
nextSettings = daySettings[1]
t = (currentHour - currentSettings.Time) / (24 - currentSettings.Time + nextSettings.Time)
end
-- Ensure t is within 0-1 range
t = clamp(t, 0, 1)
-- Interpolate lighting settings
local colorShift_Top = lerpColor3(currentSettings.ColorShift_Top, nextSettings.ColorShift_Top, t)
local colorShift_Bottom = lerpColor3(currentSettings.ColorShift_Bottom, nextSettings.ColorShift_Bottom, t)
local outdoorAmbient = lerpColor3(currentSettings.OutdoorAmbient, nextSettings.OutdoorAmbient, t)
local ambient = lerpColor3(currentSettings.Ambient, nextSettings.Ambient, t)
local cloudColor = lerpColor3(currentSettings.CloudColor, nextSettings.CloudColor, t)
-- Apply the interpolated lighting settings
Lighting.ColorShift_Top = colorShift_Top
Lighting.ColorShift_Bottom = colorShift_Bottom
Lighting.OutdoorAmbient = outdoorAmbient
Lighting.Ambient = ambient
Lighting.ClockTime = math.clamp(currentHour, 0, 24) -- Ensure valid time range
-- Update cloud colors if Terrain exists
if Terrain and Terrain:FindFirstChild("Clouds") then
Terrain.Clouds.Color = cloudColor
end
-- Wait for the next update
wait(updateInterval)
end