Script Surface Lights Inside Multiple Parts

I have a surface light inside multiple parts all grouped in one folder. I want to save time and scripts by using a single script to be able to interact with all of them at once. Specifically, I want to be able to enable them on when they are off, edit the brightness, etc.

The problem is that I don’t have the experience to make it happen. I am able to make the first SurfaceLight in my workspace in the folder where this script is to work, but every other one below ti doesn’t.

What I have tried though, is using GetChildren and attempted to use a table but that didn’t result in anything happening.

local l = b:WaitForChild("SurfaceLight")

script:WaitForChild("Dimmer").OnInvoke = function(val)
if val == 0 then
l.Enabled = false
l.Brightness = 0 * val
else
	
l.Enabled = true	
l.Brightness = 10 * val
end		

print(val)
end

Do you mean, In a single button all of the Surface Light will do something?

If so, you can use a for loop to do that.

Yeah, I thought so. Unfortunately I struggle with loops and I don’t know how to even use a loop for this? I thought loops were to literally loop something to happen?

It would be like this:

local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder

script:WaitForChild("Dimmer").OnInvoke = function(val)
if val == 0 then
    for _, lights in pairs(b:GetChildren()) do
        if lights:IsA("SurfaceLight") then
            l.Enabled = false
            l.Brightness = 0 * val
        end
    end
else
    for _, lights in pairs(b:GetChildren()) do
        if lights:IsA("SurfaceLight") then
            l.Enabled = true	
            l.Brightness = 10 * val
        end
    end
end		

print(val)
end
1 Like

Ahh, nvm! Sorry, even with what you gave me it still only targets the first surface light?

Can I see the heirarchy of the folder?

Here it is! Sorry for the late reply btw!

Sorry for my late response, (We might have different time zones)

So here is the solution:

-- By using :GetDescendants() it will take the all objects inside of a selected parent.

local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder

local function setLights(switchType)
    if switchType == true then --Open lights
        for _, lights in pairs(b:GetDescendants()) do
            if lights:IsA("SurfaceLight") then
                l.Enabled = false
                l.Brightness = 0 * val
            end
        end
    elseif switchType == false then --Close lights
        for _, lights in pairs(b:GetDescendants()) do
            if lights:IsA("SurfaceLight") then
                l.Enabled = false
                l.Brightness = 0 * val
            end
        end
    end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
if val == 0 then
    setLights(false)
else
    setLights(true)
end		

print(val)
end

I put it on a function so the modifying is easier for you and it can be re-useable for some times.

If you’re for higher please let me know when you’re free! Lol. I tried using this and it didn’t work but I am not sure if I pasted it in the right place or if there’s something else wrong.

is there any error in the console? Because I don’t actually know if you given me the full script of the code. (I can’t see the assigned value of b)

Here is the full script. The SurfaceLights are inside the “Lens1A”,“Lens2A”, etc.

local b = script.Parent:WaitForChild("NeonLighting"):WaitForChild("Lens1A","Lens2A","Lens3A","Lens4A","Lens5A","Lens6A")
local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder
local function setLights(switchType)
	if switchType == true then --Open lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				l.Enabled = false
				l.Brightness = 0 * val
			end
		end
	elseif switchType == false then --Close lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				l.Enabled = false
				l.Brightness = 0 * val
			end
		end
	end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
	if val == 0 then
		setLights(false)
	else
		setLights(true)
	end		

	print(val)
end

And in the Script Analysis it shows this.
sa

1 Like

It seems I forgot to pass the val value in the function’s parameter thats why it doesn’t work. My bad.

Here it is:

local b = script.Parent:WaitForChild("NeonLighting"):WaitForChild("Lens1A","Lens2A","Lens3A","Lens4A","Lens5A","Lens6A")
local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder
local function setLights(switchType, theValue)
	if switchType == true then --Open lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				l.Enabled = false
				l.Brightness = 10 * theValue
			end
		end
	elseif switchType == false then --Close lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				l.Enabled = false
				l.Brightness = 0 * theValue
			end
		end
	end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
	if val == 0 then
		setLights(false, val)
	else
		setLights(true, val)
	end		

	print(val)
end

Let me know if it works (or not)

So I got no errors but now none of the surface lights become enabled. :c
With my initial script I would get the very first surface light in the workspace to turn on, now not even the first one turns on.

Check my code again because I edited some of the values.

Yeah I double checked, still nothing :confused:

My bad again (I forgot to edit the l into lights):

local b = script.Parent:WaitForChild("NeonLighting"):WaitForChild("Lens1A","Lens2A","Lens3A","Lens4A","Lens5A","Lens6A")
local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder
local function setLights(switchType, theValue)
	if switchType == true then --Open lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				lights.Enabled = false
				lights.Brightness = 10 * theValue
			end
		end
	elseif switchType == false then --Close lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				lights.Enabled = false
				lights.Brightness = 0 * theValue
			end
		end
	end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
	if val == 0 then
		setLights(false, val)
	else
		setLights(true, val)
	end		

	print(val)
end

It didn’t work either but I changed the true and false parts like this and it worked but again only on the first surface light :confused:

local l = b:WaitForChild("SurfaceLight") -- From what I can see the b is the folder
local function setLights(switchType, theValue)
	if switchType == true then --Open lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				lights.Enabled = true
				lights.Brightness = 10 * theValue
			end
		end
	elseif switchType == false then --Close lights
		for _, lights in pairs(b:GetDescendants()) do
			if lights:IsA("SurfaceLight") then
				lights.Enabled = false
				lights.Brightness = 0 * theValue
			end
		end
	end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
	if val == 0 then
		setLights(false, val)
	else
		setLights(true, val)
	end		

	print(val)
end

From what I can see on the heirarchy of the model there are some SpotLights. Thats why only the SurfaceLight are detected in a for loop.

By adding another statement will fixed it:

if lights:IsA("SurfaceLight") or lights:IsA("SpotLight") then

Ah sorry! I had changed those to SurfaceLights and forgot to mention it. All of them are SurfaceLights now, i double checked to make sure.

1 Like

Also, If you are trying to get all of the Children of something, I suggest not to do this because only the first parameter will be used (for me):

local variable = parent:WaitForChild("Item1","Item2","Item3") --I dont really see any code being like this thats why only the item1 is being changed.

Overhauled the script:

local b = script.Parent:WaitForChild("NeonLighting"):GetChildren() --This will become a table of childrens {Lens1A, Lens2A, ~AndSoOn}

local function setLights(switchType, theValue)
	if switchType == true then --Open lights
		for _, lensPart in pairs(b) do
			if lensPart:IsA("Part") then 
		        for _, lights in pairs(lensPart:GetChildren()) do
		           if lights:IsA("SurfaceLight") or lights:IsA("SpotLight") then
		              lights.Enabled = true
		              lights.Brightness = 10 * theValue
		           end
		        end
 		    end
		end
	elseif switchType == false then --Close lights
		for _, lensPart in pairs(b) do
			if lensPart:IsA("Part") then 
		        for _, lights in pairs(lensPart:GetChildren()) do
		           if lights:IsA("SurfaceLight") or lights:IsA("SpotLight") then
		              lights.Enabled = false
		              lights.Brightness = 0 * theValue
		           end
		        end
 		    end
		end
	end
end

script:WaitForChild("Dimmer").OnInvoke = function(val)
	if val == 0 then
		setLights(false, val)
	else
		setLights(true, val)
	end

	print(val)
end
1 Like