Better Way To Make a Terrain Fog System?

Hello, recently I made a fog system, basically it makes the fog thicker every 600 seconds. The fog system uses a while loop and I know it probably isnt the most efficient way to do it.

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

while true do
wait(1)
TweenService:Create(Lighting,TweenInfo.new(15),{FogEnd = 1250}):Play()
wait(600)
TweenService:Create(Lighting,TweenInfo.new(15),{FogEnd = 1500}):Play()
wait(600)
TweenService:Create(Lighting,TweenInfo.new(15),{FogEnd = 2000}):Play()	
wait(600)
TweenService:Create(Lighting,TweenInfo.new(15),{FogEnd = 2250}):Play()	
wait(600)
TweenService:Create(Lighting,TweenInfo.new(15),{FogEnd = 2500}):Play()	
wait(600)
TweenService:Create(Lighting,TweenInfo.new(30),{FogEnd = 1250}):Play()	
end

Please let me know if I could do something better. Thanks!

1 Like

If you want fog that variates over time, like random fog distances, you can do a single tween in a while loop;

local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(15)

while wait(600) do
    TweenService:Create(lighting, tweenInfo, {FogEnd = math.random(1250, 2500)}):Play()
end
1 Like

You could use a variable that increases every time, and use a modulo operation on it, so it never exceeds the maximum value, and cycles back;

local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(15)
local fogEnd = 1250

while true do
    tweenService:Create(lighting, tweenInfo, {FogEnd = fogEnd}):Play()
    
    wait(600)
    
    fogEnd += 250
    if fogEnd > 2500 then -- Prefer this method instead, modulo puts it back to 0
        fogEnd = 1250
    end
end
2 Likes