I want to update a script that loads maps but there’s a problem with the lighting
It doesn’t unload it correctly like it stays in place even if one part of the script tells it to go somewhere else
Here’s the script btw :
function module.loadLevel(levelName, cloudsEnabled)
print(levelName, cloudsEnabled)
--Main variables
--local level = levelName
local lighting = game:GetService("Lighting")
local ws = game:GetService("Workspace")
--local levellighting = LevelsFolder[level].Lighting
--local levelws = LevelsFolder[level].Map
local wsassets = ws:GetChildren()
local lassets = lighting:GetChildren()
local cloud = ws.Terrain:GetChildren()
--Unload loaded level map
for i,wspace in pairs(wsassets) do
if wspace.Name == "Map" then
local level = wspace
if level and level:IsA('Instance') then
local levelFolder = level:GetAttribute("Level")
local levelfold = LevelsFolder:FindFirstChild(levelName)
if levelfold then
level.Parent = levelfold
end
end
end
end
print("Unloaded level")
--Unload lighting
--Problem probably in this zone : begin
for i,l in pairs(lassets) do
if l.Name == "Atmosphere" or l.Name == "Sky" or l.Name == "Bloom" or l.Name == "Blur" or l.Name == "ColorCorrection" or l.Name == "SunRays" or l.Name == "DepthOfField" then
if l:GetAttribute("Level") == levelName then
l.Parent = LevelsFolder:FindFirstChild(levelName).Lighting
end
end
end
--Problem probably in this zone : end
print("Unloaded lighting")
--Unload clouds
local normalCloud = workspace.Terrain:FindFirstChild("Clouds")
if normalCloud then
for i,c in pairs(cloud) do
if c.Name == "Clouds" then
local level = c
if level and level:IsA('Instance') then
local levelFolder = level:GetAttribute("Level")
local levelfold = LevelsFolder:FindFirstChild(levelName)
if levelfold then
level.Parent = levelfold.Lighting
end
end
end
end
else
print("No clouds.")
end
print("Unloaded clouds")
--Unload terrain
game.Workspace.Terrain:Clear()
print("Unloaded terrain")
--Load terrain
for i,lvl in pairs(Levels) do
if lvl.Name == levelName then
workspace.Terrain:PasteRegion(lvl.Map[levelName.."MapTerrain"], workspace.Terrain.MaxExtents.Min, true)
end
end
print("loaded terrain")
--Load map
for i,lvl in pairs(Levels) do
if lvl.Name == levelName then
local level = lvl
if level and level:IsA("Instance") then
level:WaitForChild("Map").Parent = workspace
end
end
end
print("loaded map")
--Load lighting
for i,lvl2 in pairs(Levels) do
if lvl2.Name == levelName then
local level = lvl2
if level and level:IsA("Instance") then
local lfolder = level:WaitForChild("Lighting")
local lasset = lfolder:GetChildren()
for i,lightingassets in pairs(lasset) do
if lightingassets.Name == "Atmosphere" or lightingassets.Name == "DepthOfField" or lightingassets.Name == "Sky" or lightingassets.Name == "Bloom" or lightingassets.Name == "Blur" or lightingassets.Name == "ColorCorrection" or lightingassets.Name == "SunRays" then
lightingassets.Parent = lighting
end
end
lighting.Ambient = level.Lighting.Amb.Value
lighting.Brightness = level.Lighting.Brightness.Value
lighting.ColorShift_Bottom = level.Lighting.CS_Bottom.Value
lighting.ColorShift_Top = level.Lighting.CS_Top.Value
lighting.EnvironmentDiffuseScale = level.Lighting.EnvDiff.Value
lighting.EnvironmentSpecularScale = level.Lighting.EnvSpec.Value
lighting.OutdoorAmbient = level.Lighting.OutdoorAmb.Value
lighting.ShadowSoftness = level.Lighting.ShadowSoftness.Value
lighting.ClockTime = level.Lighting.ClockTime.Value
lighting.GeographicLatitude = level.Lighting.GeoLat.Value
end
end
end
print("loaded lighting")
--Load clouds if enabled
if cloudsEnabled == true then
for i,lvl in pairs(Levels) do
if lvl.Name == levelName then
local level = lvl
if level and level:IsA("Instance") then
level.Lighting.Clouds.Parent = workspace.Terrain
end
end
end
end
print("loaded clouds")
warn(levelName .. " has finished loading !")
end
Please if anyone sees this help me fix it cause it’s been taking over my mind (and it’s just painful to fix)