
KeylessUniversal

local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
-- Nomes para limpeza de "lixo" antigo
local ATMOSPHERE_NAME = "Otimizado_Atmosphere"
local SUNRAYS_NAME = "Otimizado_SunRays"
-- Variáveis globais para armazenar referências dos objetos principais
local Atmosphere, SunFlare
-- Função para limpar resquícios anteriores (Garbage Collection manual)
local function limparLixoAnterior()
local cleanupCount = 0
for _, child in ipairs(Lighting:GetChildren()) do
if child.Name == ATMOSPHERE_NAME or child.Name == SUNRAYS_NAME or child:IsA("Atmosphere") then
child:Destroy()
cleanupCount = cleanupCount + 1
end
end
-- Verificação de sobrecarga - limpar se tiver mais de 10 objetos
if cleanupCount > 0 then
print("Limpando", cleanupCount, "objetos antigos")
end
end
-- Função para criar objetos principais apenas se não existirem
local function criarObjetosSeNecessario()
-- Verificar se já existe
local existeAtmosphere = false
local existeSunRays = false
for _, child in ipairs(Lighting:GetChildren()) do
if child.Name == ATMOSPHERE_NAME then
existeAtmosphere = true
Atmosphere = child
elseif child.Name == SUNRAYS_NAME then
existeSunRays = true
SunFlare = child
end
end
-- Criar apenas se não existirem
if not existeAtmosphere then
Atmosphere = Instance.new("Atmosphere")
Atmosphere.Name = ATMOSPHERE_NAME
Atmosphere.Parent = Lighting
end
if not existeSunRays then
SunFlare = Instance.new("SunRaysEffect")
SunFlare.Name = SUNRAYS_NAME
SunFlare.Spread = 1
SunFlare.Parent = Lighting
end
end
-- CONFIGURAÇÃO DA ILUMINAÇÃO (Executado apenas uma vez)
Lighting.Technology = Enum.Technology.Future
Lighting.GlobalShadows = true
Lighting.ShadowSoftness = 1
Lighting.Ambient = Color3.new(0, 0, 0)
Lighting.OutdoorAmbient = Color3.new(0, 0, 0)
Lighting.EnvironmentDiffuseScale = 0.35
Lighting.EnvironmentSpecularScale = 0.5
Lighting.ClockTime = 13.5 -- Horário inicial
-- Limpar lixo inicial e criar objetos
limparLixoAnterior()
criarObjetosSeNecessario()
-- PRÉ-DEFINIÇÃO DE CORES
local NightFog = Color3.fromRGB(21, 25, 31)
local NightAtmosphere = Color3.fromRGB(86, 100, 115)
local NightDecay = Color3.fromRGB(28, 36, 43)
local DawnFog = Color3.fromRGB(120, 135, 150)
local DawnAtmosphere = Color3.fromRGB(170, 185, 200)
local DawnDecay = Color3.fromRGB(100, 115, 130)
local DayFog = Color3.fromRGB(190, 200, 210)
local DayAtmosphere = Color3.fromRGB(200, 210, 220)
local DayDecay = Color3.fromRGB(150, 160, 170)
-- Variáveis para controle da névoa
local nevoaIntensidade = 0
local nevoaMaxima = 0.8 -- Névoa máxima para o final da noite
-- Variáveis de controle do loop principal
local lastState = ""
local lastUpdate = 0
local lastFogUpdate = 0
-- LOOP PRINCIPAL OTIMIZADO (Roda em thread separada)
task.spawn(function()
while true do
local t = Lighting.ClockTime
local currentTime = tick()
local currentState = ""
-- Determinar o estado atual com intervalos corretos: noite, transição, dia
if t >= 5 and t < 7 then
currentState = "Amanhecer"
elseif t >= 17 and t < 19 then
currentState = "Entardecer"
elseif t >= 19 or t < 5 then
currentState = "Noite"
else
currentState = "Dia"
end
-- Só aplica as mudanças se o estado mudou (Otimização Máxima)
if currentState ~= lastState then
lastState = currentState
-- Aplicar configurações baseadas no estado
if currentState == "Noite" then
Lighting.FogColor = NightFog
Lighting.Brightness = 1
Lighting.ExposureCompensation = 1
Atmosphere.Color = NightAtmosphere
Atmosphere.Decay = NightDecay
Atmosphere.Density = 0.025
SunFlare.Intensity = 0.5
elseif currentState == "Amanhecer" then
Lighting.FogColor = DawnFog
Lighting.Brightness = 1
Lighting.ExposureCompensation = 1
Atmosphere.Color = DawnAtmosphere
Atmosphere.Decay = DawnDecay
Atmosphere.Density = 0.03
SunFlare.Intensity = 0.5
elseif currentState == "Entardecer" then
Lighting.FogColor = DawnFog
Lighting.Brightness = 1
Lighting.ExposureCompensation = 1
Atmosphere.Color = DawnAtmosphere
Atmosphere.Decay = DawnDecay
Atmosphere.Density = 0.03
SunFlare.Intensity = 0.5
elseif currentState == "Dia" then
Lighting.FogColor = DayFog
-- Sol mais forte
Lighting.Brightness = 1.2
Lighting.ExposureCompensation = 0.2
Atmosphere.Color = DayAtmosphere
Atmosphere.Decay = DayDecay
Atmosphere.Density = 0.03
SunFlare.Intensity = 0.6
end
end
-- Sistema de névoa suave e lenta (apenas quando está escuro)
if currentTime - lastFogUpdate > 0.5 then -- Atualiza névoa a cada meio segundo para menos overhead
lastFogUpdate = currentTime
if currentState == "Noite" or currentState == "Amanhecer" or currentState == "Entardecer" then
-- Calcular intensidade da névoa com base no horário
local nevoaBase = 0.05 -- Névoa mínima
local horaAtual = t
-- Criar efeito de névoa que aumenta lentamente até o fim da noite
if horaAtual >= 19 then
-- Noite completa: névoa aumenta progressivamente
nevoaIntensidade = (horaAtual - 19) / 10 * nevoaMaxima
nevoaIntensidade = math.min(nevoaIntensidade, nevoaMaxima)
elseif horaAtual < 5 then
-- Madrugada (antes de 5h): névoa máxima
nevoaIntensidade = nevoaMaxima
else
-- Transição: névoa suave
nevoaIntensidade = nevoaBase
end
-- Aplicar névoa (com suavização)
Lighting.FogEnd = 1000 + (nevoaIntensidade * 5000) -- Ajuste do alcance
Lighting.FogStart = 50 + (nevoaIntensidade * 100) -- Ajuste do início
Lighting.FogColor = NightFog -- Manter a cor da névoa escurecida
else
-- Dia: névoa mínima
Lighting.FogEnd = 10000
Lighting.FogStart = 1000
nevoaIntensidade = 0
end
end
-- Verificação automática de sobrecarga (limpar lixo a cada 60 segundos)
if currentTime - lastUpdate > 60 then
limparLixoAnterior()
lastUpdate = currentTime
-- Verificar se ainda existem os objetos principais
local objetosEncontrados = 0
for _, child in ipairs(Lighting:GetChildren()) do
if child.Name == ATMOSPHERE_NAME or child.Name == SUNRAYS_NAME then
objetosEncontrados = objetosEncontrados + 1
end
end
-- Se não encontrar objetos principais, recriá-los
if objetosEncontrados == 0 then
criarObjetosSeNecessario()
end
end
-- Atualiza a cada segundo para evitar overhead (sem loop infinito pesado)
task.wait(1)
end
end)
-- Removido RunService.Heartbeat que causava conflito
-- O sistema agora depende exclusivamente do loop principal optimizadoNo comments yet for this script.



