I am trying to make a time script that will change the ambient when it gets to a certain point... Here is what I have so far
while true do game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+.5 ) wait(.05) end nighttime = 1080 morningtime = 360 Mins = game.Lighting:GetMinutesAfterMidnight() ambient = game.Lighting.Ambient function Sleep() while ambient > color3.new(80,80,80) do ambient = ambient - color3.new( 1,1,1) wait(.5) end end function Wake() while ambient < color3.new(130,130,130) do ambient = ambient + color3.new( 1,1,1) wait(.5) end end if mins >= nighttime or mins <= morningtime then Sleep() end if mins <= nighttime or mins >= morningtime then Wake() end
The Color3 constructor is Color3.new
, not color3.new
(case matters).
Despite what the Properties tab will show you, the Color3.new
constructor takes numbers 0 to 1, not 0 to 255.
Divide each of the parameters by 255:
Color3.new(80/255,80/255,80/255)
Color3 values do not support adding or substring with +
and -
. You must explicitly reconstruct them yourself. Since that is length, we should make a support function for it:
function addColor3s( a, b ) return Color3.new( a.r + b.r, a.g + b.g, a.b + b.b ); end function subColor3s( a, b ) return Color3.new( a.r - b.r, a.g - b.g, a.b - b.b ); end
... ambient = subColor3s(ambient , Color3.new( 1,1,1) ); ...
Neither is >
or <
. In fact, you cannot order them, so this doesn't even make sense. Check a component, e.g.,
while ambient.r > 80/255 do
You assign to ambient
several times. This will change the value of ambient
. However, it just changes the value at the name ambient
-- this will not affect the Lighting at all, because you are not telling it to.
The assignment to game.Lighting.Ambient
gives ambient
the value of the what Lighting's ambient color was. It only sets ambient
to a color. It does not somehow "link" the property with the variable.
After modifying ambient
, write it back out to the lighting's ambient:
ambient = (whatever) game.Lighting.Ambient = ambient;
Also note that your while
loop at the top never ends. That means everything after the first five lines never is executed. Move your function definitions to the top (definitions always belong up). In addition, move your if
checks to Sleep()
or Wake()
to within the loop, so that they are appropriately constantly checked (as well as the computation for mins
).
Again, variable names are case sensitive, so mins
is not the same thing as Mins
. Pick one and use it (the Lua canonical name would be mins
)
Structures like you have are usually bad, if they can be avoided. This is brittle for a few reasons:
Execution order matters. You do the transition in ambient which interrupts the day / night cycle
Syncing the day / night becomes difficult with the ambient transition (even with my changes this script will behave poorly, you'll see)
Execution time matters -- matching rates and durations and pauses is difficult or impossible.
Clearly what you want is for the ambient to be a result of what time it is. In that case, we should explicitly do that! It's even simpler since the ambient is always a gray, so much less to keep track of.
We have to add one more setting, the duration of the transition
from night to day, let's say
transition = 60 -- "minutes"
During the night ( mins >= nighttime or mins <= morningtime
) the brightness is 80 / 255
.
During the day (mins <= nighttime - transition or mins >= morningtime + transition
) the brightness is 130 /255
.
In between, we just use "linear interpolation." We multiply our progress by the change required, and add that to where we started: 80 + progress * (130 - 80)
.
local progress = 1; -- Day time if mins >= nighttime or mins <= morningtime then progress = 0; -- Night time end if mins > nighttime - transition and mins < nighttime then -- Sun set local start = nighttime - transition; local stop = nighttime; local progress = (mins - start) / (stop - start); progress = 1 - progress; -- Reverse progress order from night -- > day end if mins > morningtime and mins < morningtime + transition then -- Sun rise local start = morningtime; local stop = morningtime + transition; local progress = (mins - start) / (stop - start); end local brightness = 80 + progress * (130 - 80); brightness = brightness / 255; game.Lighting.Ambient = Color3.new(brightness, brightness, brightness);
This code just operates on the current time in minutes. It doesn't do any delaying or unnecessary changes or calculations, so it works without making any fuss alongside something modifying the time of day.