Problem changing the Ambient according to the time of day

Hello everyone I have a problem that I do not understand, my goal is to change the properties “Ambient” and “OutDoorAmbient” depending on the time of day. I watched the other posts about this topic but nothing works, I really can not understand why my script does not work.

I hope someone could help me thank you in advance.

The Script:

minute = game.Lighting:GetMinutesAfterMidnight()

while true do 
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+ 1)
	wait(0)    --1 min = 4,778761062
end
	


while true do
	
if game.minute >= 360 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if game.minute >= 420 then
		game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138) 
	wait()
end

if minute >= 1020 then
	       game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		   game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138)
		wait()
	end
	
	if minute >= 1080 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if game.minute >= 1110 then
		game.Lighting.Ambient = Color3.fromRGB (38,38,38)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(44,44,44) 
	wait()
end

if game.minute >= 600 then
		game.Lighting.Ambient = Color3.fromRGB (150,150,150)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(200,200,200) 
	wait()
end	

end

1 Like

You have two while loops. The second one will never run unless you call break in the first. Put all your code in one loop and see if it works.

Hi thank you for responding, I tried to put in the same loop but only one Ambient worked. Or else all Ambient was activated at the same time (with all wait ())

Paste your updated code so I can take a look.

30 chars…

Alternatively, you can put the first loop inside a coroutine like so;

coroutine.resume(coroutine.create(function()
    while true do
        -- do stuff
    end
end))

while true do
    -- do stuff
end

This creates a separate thread for the first loop, so that the rest of the script can continue running without yielding.

1 Like

minute = game.Lighting:GetMinutesAfterMidnight()

while true do 
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+ 1)
	wait(0)    --1 min = 4,778761062
	
	if game.minute >= 360 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if game.minute >= 420 then
		game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138) 
	wait()
end

if minute >= 1020 then
	       game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		   game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138)
		wait()
	end
	
	if minute >= 1080 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if game.minute >= 1110 then
		game.Lighting.Ambient = Color3.fromRGB (38,38,38)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(44,44,44) 
	wait()
end

if game.minute >= 600 then
		game.Lighting.Ambient = Color3.fromRGB (150,150,150)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(200,200,200) 
	wait()
end	

end

I will try immediately thank you

1 Like

Why are you saying game.minute when you have a variable minute?

Oh yes that’s a mistake because before I called game.Lighting:GMAM

I forgot to remove game

You could try removing the waits in each if statement as well. Then just move the wait from the top to the bottom and see if that works (I just can’t see whats wrong). Unless you already got it to work.

Here’s the problem: The Ambient change all at the same time I think it is a problem interval for me but how to fix it?

script:

 minute = game.Lighting:GetMinutesAfterMidnight()




coroutine.resume(coroutine.create(function()
while true do
    game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+ 1)
	wait(0)    --1 min = 4,778761062
 end
end))

while true do 
	
	if minute >= 360 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if minute >= 420 then
		game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138) 
	wait()
end

if minute >= 1020 then
	       game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		   game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138)
		wait()
	end
	
	if minute >= 1080 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	wait()
end

if minute >= 1110 then
		game.Lighting.Ambient = Color3.fromRGB (38,38,38)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(44,44,44) 
	wait()
end

if minute >= 600 then
		game.Lighting.Ambient = Color3.fromRGB (150,150,150)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(200,200,200) 
	wait()
end	

end

put a wait between the other while loop? Then it will have an offset at least.

You are relying on the variable minute, which was only set one time and not checked again. When you set it to the function it just returned a value, it doesn’t check it again when you call in minute later.

You need to add

minute = game.lighting:GetMinutesAfterMidnight()

at the beginning or end of your second loop.

I did it but still the same problem everything changes at the same time

	if minute >= 360 and minute <= 420 then

Can i do that ?

That is because you are doing them as all separate if statements. Instead try using elseif.

Yes you can that will work too.

Ok Thanks, but i can use elseif as much as I want for one If ?

Okay with two intervals it does not work

I tried to use elseif but the change of Ambient and OutDoorAmbient only works for the first If

while true do

	minute = game.Lighting:GetMinutesAfterMidnight()	

	if minute >= 360 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	


   elseif minute >= 420 then
		game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138) 
	


   elseif minute >= 1020 then
	       game.Lighting.Ambient = Color3.fromRGB (106,106,106)
		   game.Lighting.OutdoorAmbient = Color3.fromRGB(138,138,138)
		
	
	
   elseif minute >= 1080 then
		game.Lighting.Ambient = Color3.fromRGB (70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(84,84,84) 
	


   elseif minute >= 1110 then
		game.Lighting.Ambient = Color3.fromRGB (38,38,38)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(44,44,44) 
	


   elseif minute >= 600  then
		game.Lighting.Ambient = Color3.fromRGB (150,150,150)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(200,200,200) 
	
end	

end