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
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?
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
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.
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
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
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.
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
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
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