-- Ambient settings


	-- Morning
	morning_Brightness = 1.2
	morning_Ambient = Color3.new(75/255, 83/255, 78/255)
	morning_OutdoorAmbient = Color3.new(113/255, 128/255, 119/255)
	morning_FogColor = Color3.new(207/255, 226/255, 201/255)
	morning_FogEnd = 2500

	-- Sunset
	sunset_Brightness = 0.8
	sunset_Ambient = Color3.new(59/255,26/255,59/255)
	sunset_OutdoorAmbient = Color3.new(143/255,97/255,38/255)
	sunset_FogColor = Color3.new(191/255,126/255,15/255)
	sunset_FogEnd = 2500

	-- Night
	night_Brightness = 0
	night_Ambient = Color3.new(18/255,18/255,18/255)
	night_OutdoorAmbient = Color3.new(50/255, 55/255, 61/255)
	night_FogColor = Color3.new(5/255, 0, 13/255)
	night_FogEnd = 1500

	-- Day
	day_Brightness = 1
	day_Ambient = Color3.new(162/255, 162/255, 162/255)
	day_OutdoorAmbient = Color3.new(162/255, 162/255, 162/255)
	day_FogColor = Color3.new(122/255, 234/255, 244/255)
	day_FogEnd = 4000



local l = game:service("Lighting")
lights = game.Workspace:FindFirstChild("Lights")

function turnLights(lightswitch)
	local lamps = lights:GetChildren()
	for i = 1, #lamps do
		if lamps[i]:FindFirstChild("Case") then
			if lamps[i]:FindFirstChild("Case"):FindFirstChild("Light") then
				lamps[i]:FindFirstChild("Case"):FindFirstChild("Light").Enabled = lightswitch
			end
		end
	end
end

function fadeAmbient(startval, endval, duration)
	for i = 1, duration do
		local setR = startval.r + ((endval.r-startval.r)/duration)*i
		local setG = startval.g + ((endval.g-startval.g)/duration)*i
		local setB = startval.b + ((endval.b-startval.b)/duration)*i
		l.Ambient = Color3.new(setR, setG, setB)
		wait(0.05)
	end
end
function fadeOutdoorAmbient(startval, endval, duration)
	for i = 1, duration do
		local setR = startval.r + ((endval.r-startval.r)/duration)*i
		local setG = startval.g + ((endval.g-startval.g)/duration)*i
		local setB = startval.b + ((endval.b-startval.b)/duration)*i
		l.OutdoorAmbient = Color3.new(setR, setG, setB)
		wait(0.05)
	end
end
function fadeFogColor(startval, endval, duration, fogstart, fogreach)
	for i = 1, duration do
		local setR = startval.r + ((endval.r-startval.r)/duration)*i
		local setG = startval.g + ((endval.g-startval.g)/duration)*i
		local setB = startval.b + ((endval.b-startval.b)/duration)*i
		l.FogColor = Color3.new(setR, setG, setB)
		l.FogEnd = fogstart + ((fogreach-fogstart)/duration)*i
		wait(0.06)
	end
	l.FogEnd = fogreach
end




timeOfDay = "Day"
timeEffectsSettled = "Setup"
while true do

	if (l:GetMinutesAfterMidnight() >= ot) and (l:GetMinutesAfterMidnight() <= ct) then
		l:setMinutesAfterMidnight(l:getMinutesAfterMidnight()+dayspeed)
		wait(0.1)
	elseif (l:GetMinutesAfterMidnight() <= ot) or (l:GetMinutesAfterMidnight() >= ct) then
		l:setMinutesAfterMidnight(l:getMinutesAfterMidnight()+nightspeed)
		wait(0.1)
	end
	tod = string.sub(l.TimeOfDay, 1, 2)
	if tod == "17" then
		timeOfDay = "sunset"
	end
	if (l:GetMinutesAfterMidnight() >= morning_time2) and (l:GetMinutesAfterMidnight() <= sunset_time1) then
		timeOfDay = "Day"
	elseif (l:GetMinutesAfterMidnight() >= night_time1) and (l:GetMinutesAfterMidnight() <= 1440) or 
		 (l:GetMinutesAfterMidnight() >= 0) and (l:GetMinutesAfterMidnight() <= night_time2) then
		timeOfDay = "Night"
	elseif (l:GetMinutesAfterMidnight() >= morning_time1) and (l:GetMinutesAfterMidnight() <= morning_time2) then
		timeOfDay = "Morning"
	elseif (l:GetMinutesAfterMidnight() >= sunset_time1) and (l:GetMinutesAfterMidnight() <= sunset_time2) then
		timeOfDay = "Sunset"
	end
	
	coroutine.resume(coroutine.create(function()  end))

	if timeEffectsSettled ~= timeOfDay then
		if timeOfDay == "Day" then
			turnLights(false)
			l.FogStart = 1000
			local duration = 200
			coroutine.resume(coroutine.create(function() fadeAmbient(l.Ambient, day_Ambient, duration) end))
			coroutine.resume(coroutine.create(function() fadeOutdoorAmbient(l.OutdoorAmbient, day_OutdoorAmbient, duration) end))
			coroutine.resume(coroutine.create(function() fadeFogColor(l.FogColor, day_FogColor, duration, l.FogEnd, day_FogEnd) end))

		elseif timeOfDay == "Night" then
			turnLights(true)
			l.FogStart = 0
			local duration = 200
			coroutine.resume(coroutine.create(function() fadeAmbient(l.Ambient, night_Ambient, duration) end))
			coroutine.resume(coroutine.create(function() fadeOutdoorAmbient(l.OutdoorAmbient, night_OutdoorAmbient, duration) end))
			coroutine.resume(coroutine.create(function() fadeFogColor(l.FogColor, night_FogColor, duration, l.FogEnd, night_FogEnd) end))

		elseif timeOfDay == "Morning" then
			turnLights(false)
			l.FogStart = 600
			duration = 150
			coroutine.resume(coroutine.create(function() fadeAmbient(l.Ambient, morning_Ambient, duration) end))
			coroutine.resume(coroutine.create(function() fadeOutdoorAmbient(l.OutdoorAmbient, morning_OutdoorAmbient, duration) end))
			coroutine.resume(coroutine.create(function() fadeFogColor(l.FogColor, morning_FogColor, duration, l.FogEnd, morning_FogEnd) end))

		elseif timeOfDay == "Sunset" then
			turnLights(true)
			l.FogStart = 200
			duration = 240
			coroutine.resume(coroutine.create(function() fadeAmbient(l.Ambient, sunset_Ambient, duration) end))
			coroutine.resume(coroutine.create(function() fadeOutdoorAmbient(l.OutdoorAmbient, sunset_OutdoorAmbient, duration) end))
			coroutine.resume(coroutine.create(function() fadeFogColor(l.FogColor, sunset_FogColor, duration, l.FogEnd, sunset_FogEnd) end))
		end
		timeEffectsSettled = timeOfDay
	end
end