The Parent property of Atmosphere is locked, current parent: NULL, new parent Lighting

I’ve been trying to make a gui that would change the lighting when a textbutton is clicked

it works most of the time, until it doesn’t

Once I change it, around the 5’th time, it says this error:

“The Parent property of Atmosphere is locked, current parent: NULL, new parent Lighting”

here is a sample of some of my scripts.

local Lighting = game:GetService("Lighting")
local At = script:WaitForChild("Atmosphere")

At.Parent = Lighting

Just to make it clear, this is for a plugin.

If there is anyone that can help me, it will be really appreciated.

Thanks for your time.

Can you provide more of the script? This on its own shouldn’t be causing anything. It sounds like the Atmosphere is getting :Destroy()ed at some point, since using Destroy locks the parent.

Once a lighting changer textbutton is clicked, it puts lighting elements as childrens of ligthing
then when another lighting changer textbutton is clicked, it deletes those elements with an if statement, checking if it already exists. then adds in the lighting elements.

Good chance the atmosphere object or its container is being destroyed.

It waits 1 second after the children of lighting is deleted before it adds in lighting

Have you tried combatting the issue with a longer yield? Might be worth sharing the relevant code so we can better understand your problem.

This is a sample of the script that deletes the children of lighting

function OnClicked()
wait(0.5)
	local c = game:GetService("Lighting"):GetChildren()

	for i = 1,#c do

		if c[i].ClassName == "Atmosphere" then

			c[i]:Destroy()

		end

	end
end 

script.Parent.MouseButton1Down:connect(OnClicked)

Cloning the atmosphere as a variable still shows the same problem.

Add another line below c[i]:Destroy():

c[i]:Destroy()
c[i] = nil

I have encountered a similar issue, this should work.