Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make OutdoorAmbient gradually get darker over time?

Asked by
snarns 25
6 years ago

So I have a script that changes how dark it is outside, but I would like for it to gradually get dark outside, rather than have it be light one moment and then very dark the next. Is there any way I can do this?

This script controls the time:

while wait (.5) do
    game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight() +1)
end

And here is the script that changes OutdoorAmbient and FogColor:

b = game.Lighting

local oh,om = 7,00  -- Place gets dark at this time
local ch,cm = 18,30 -- Place becomes light again at this time

local l = game:service("Lighting")
if (om == nil) then om = 0 end
if (cm == nil) then cm = 0 end


function TimeChanged()
    local ot = (oh + (om/60)) * 60
    local ct = (ch + (cm/60)) * 60
    if (ot < ct) then
        if (l:GetMinutesAfterMidnight() >= ot) and (l:GetMinutesAfterMidnight() <= ct) then
b.OutdoorAmbient = Color3.fromRGB(135,135,135) -- Morning ambient
b.FogColor = Color3.fromRGB(107,115,93) -- Morning fog
        else
b.OutdoorAmbient = Color3.fromRGB(10,10,10) -- Night ambient
b.FogColor = Color3.fromRGB(10,10,10) -- Night fog
        end
    elseif (ot > ct) then
        if (l:GetMinutesAfterMidnight() >= ot) or (l:GetMinutesAfterMidnight() <= ct) then
b.OutdoorAmbient = Color3.fromRGB(135,135,135)
b.FogColor = Color3.fromRGB(107,115,93)
        else
b.OutdoorAmbient = Color3.fromRGB(10,10,10)
b.FogColor = Color3.fromRGB(10,10,10)
        end
    end
end

TimeChanged()
game.Lighting.Changed:connect(function(property)
            if (property == "TimeOfDay") then
                TimeChanged()
            end
        end)

2 answers

Log in to vote
0
Answered by 6 years ago

Its gonna take some time, but do something like a

wait(5) darkness = (5,5,5)

wait(5) darkness = (4.4.4)

0
I'm not sure how to apply this to my script, but surely there's a better way, right? snarns 25 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

To fade gradually into a different outdoor ambient, you can use a for loop, and some color interpolation.

Luckily, ROBLOX has color interpolation, so you can use that.

In your for loop, you'll need to make it increment by a small value, and make it start at 0 and stop at 1. The smaller your step value, the longer the fade will last.

You can use Color3.fromRGB(), as you have done. You'll need to define your starting value, and :lerp() will do the rest, mostly.

Here's the example, from totally black to magenta:

local start = Color3.fromRGB(0, 0, 0)
for i = 0, 1, 0.01 do
    game.Lighting.OutdoorAmbient = start:lerp(Color3.fromRGB(255, 0, 255), i)
    wait()
end

:lerp() is linear interpolation. There's versions of it for CFrame and Vector3. It transitions from one value to another with the "position" in argument 2.

Answer this question