My brain is melting on a random bug

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)

2 Likes

4 hours since you posted, and I’m first to reply.

I get why your brain is melting. You’re dealing with large chunk of code. Imagine you having to do an entire book of math practice questions at the same time. Don’t do that.

I’m here to give you a direction, because sometimes, scripts are too complex and require crazy amount of context. So, I don’t read your script, I give you a direction to debug and solve the problem yourself.

Idea

How about break the script into chunks and testing if the first part works. If it does, then move on to the next chunk. Output and print statements should help A LOT here.

Keep doing this, and then you’ll eventually be able to “corner” and narrow the problem down. And finally, you’ll be like “OHHHHH, so that’s the problem!”

but there’s a problem with the lighting

Maybe this could hint that something is wrong in the a chunk of the code that handles the lighting?

1 Like

oh well this helped me finding the bugs because there were multiple bugs on it
and i also managed to fix them all

well thanks for your help, i’m gonna set this as a solution

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.