Day / Night Script works, but it doesn't lose Brightness and darken Ambience when it's supposed to

My goal for this script is that I want to make this script to change the Brightness, Ambience and the Outdoor Ambience once it reaches 6PM in game, or 18 in ClockTime to make it feel like when it reaches that time as Night-Time but not as bright when Brightness is at 2.

Yet, somehow it doesn’t work

Here is what it looks like when I make it run/play when In Game

I’ve tried to use the “L variable” along with the “Light variable” to try changing the Brightness, Ambience and Outdoor Ambience of Lighting, yet didn’t work as what it’s supposed to and I am confused on how it is not working.

I’ve also used the “L variable” instead of the “Light variable” and it still didn’t work.

Here is the script:

-- Variables

L = game:GetService("Lighting")

Light = game.Lighting
CT = game.Lighting.ClockTime

-- Time Ambience Variables

Day_Am = Color3.fromRGB(70, 70, 70)
Night_Am = Color3.fromRGB(40, 40, 40)

-- Actions

while true do 
	
	wait(.0005)
	
	T = L:GetMinutesAfterMidnight()
	L:SetMinutesAfterMidnight(T + .75)
	
end

while true do
	
	wait(1)
	
	if CT == 6.25 then

		Light.Brightness = 2
		Light.OutdoorAmbient = Day_Am
		Light.Ambient = Day_Am

	else if CT == 18 then

			Light.Brightness = 0
			Light.OutdoorAmbient = Night_Am
			Light.Ambient = Night_Am
			
		end
		
	end
end

i think the issue is that ClockTime changes too fast before the script can check it, try merging both of them into one while loop instead

edit: also try updating the ClockTime variable inside the loop

1 Like

The first while loop is cancelling the one after. Either use Coroutines or merge them into the same loop.

1 Like

Couple problems. (1) When the script executes, the first while loop is a “while true” so it is never going to stop looping. This is preventing the code beyond it from executing, using “print” statements will help you diagnose your code. (2) the time is not going to be exactly 6.25 or 18. Use greater than or less than operators when trying to see if a number has crossed a threshold value. Anyway, here is an updated version of your script that will correctly do day/night shifts…

L = game:GetService("Lighting")
Light = game.Lighting


-- Time Ambience Variables

Day_Am = Color3.fromRGB(70, 70, 70)
Night_Am = Color3.fromRGB(40, 40, 40)

local keepRunning = true
function timeUpdateWorker()
	local T
	while keepRunning do 
		wait()
		T = L:GetMinutesAfterMidnight()
		L:SetMinutesAfterMidnight(T + .75)
	end
end
-- This runs the timeUpdateWorker function in its own thread
-- keepRunning = false will cancel it
spawn(timeUpdateWorker)

local day = false
local nextDay = true

while true do
	wait(1)
	local CT = game.Lighting.ClockTime
	
	if not nextDay and CT < 3 then
		-- Reset day flag each time clock rolls over
		day = false
		nextDay = true
	elseif nextDay and not day and CT > 6.25 then
		print("Setting Day")
		day = true
		Light.Brightness = 2
		Light.OutdoorAmbient = Day_Am
		Light.Ambient = Day_Am
	elseif day and CT > 18 then
		print("Setting Night")
		day = false
		nextDay = false
		Light.Brightness = 0
		Light.OutdoorAmbient = Night_Am
		Light.Ambient = Night_Am
	end
end
3 Likes