So the point is that I’m making a electric generator system, and the script works and goes well but the problem is that when the the lights goes off (these are inside a model called Light Switch), only the range number is subtracted and when they get back on, the range number is added. When the lights are off, the lights still neon material and I would want them to change to concrete material along the light’s range when these are off. When these get back on, then I would want the lights to change back again to neon along the light’s range when these are on.
Here is the script:
DeBounce = false
Model = script.Parent.Parent
local lights = {}
local function FindLights(obj)
for _, child in pairs(obj:GetChildren()) do
if child:IsA("Light") then
table.insert(lights, child)
end
FindLights(child)
end
end
FindLights(game.Workspace["Light Switch"].Lights)
function OnClick(Player)
if script.Value.Value == 1 then
if Player:GetRankInGroup(4392500) >= 0 or Player:GetRankInGroup(2694395) >= 0 then
if DeBounce == false then
DeBounce = true
Model.Status.Sound:Play()
wait(1.3)
Model.Screen.BrickColor = BrickColor.new("Really black")
Model.Status.BrickColor = BrickColor.new("Really red")
for _, light in pairs(lights) do
light.Range = light.Range - 15
end
game.Lighting.FogEnd = game.Lighting.FogEnd - 100
game.Workspace["Light Switch"].PowerDown:Play()
Model.Exhaust.Smoke.Enabled = false
repeat Model.Body.Sound.Volume = Model.Body.Sound.Volume - .1
wait(.1)
until Model.Body.Sound.Volume <= 0
wait(1)
script.Value.Value = 2
DeBounce = false
else
end
else
end
elseif script.Value.Value == 2 then
if Player:GetRankInGroup(4392500) >= 0 or Player:GetRankInGroup(2694395) >= 0 then
if DeBounce == false then
DeBounce = true
Model.Status.Sound:Play()
wait(1.3)
Model.Screen.BrickColor = BrickColor.new("White")
Model.Status.BrickColor = BrickColor.new("Lime green")
for _, light in pairs(lights) do
light.Range = light.Range + 15
end
game.Lighting.FogEnd = game.Lighting.FogEnd + 100
game.Workspace["Light Switch"].PowerUp:Play()
Model.Exhaust.Smoke.Enabled = true
repeat Model.Body.Sound.Volume = Model.Body.Sound.Volume + .1
wait(.1)
until Model.Body.Sound.Volume >= 1
wait(1)
script.Value.Value = 1
DeBounce = false
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(OnClick)
local function ChangeMaterialForModel(model, newMaterial)
local parts = {}
You would create something such as the following:
local function ChangeMaterialForModel(model, newMaterial)
local parts = model:GetChildren() --creates a table with all of the Children under a given Parent, such a workspace or model.
for _, object in parts do --loops through the table, object being the Child, the index is not used, so a placeholder '_' is used
if object:IsA("Part") then --checks if the object is actually classed as a part instance
object.Material = newMaterial --as object is a part, it changes the material
end
end
end
ChangeMaterialForModel(workspace.lights, "Neon") --calls the function with the ideal model and material
I only add notes here just in case you don’t know something, but I assume you do.
This works for something like this:
Nope, the last video I sent the lights weren’t getting it’s range substracted and the material didn’t change.
I’m not that good at scripting but I do understand some concepts of it.
Here’s an image of how the lights are in the model of Light Switch since it’s the main model to change the spotlights range.
Btw I don’t know if I said it before but the generator has a random event that it just breaks in a random moment so the player has to repair it. It happens the exact same way as if the player just turns off the generator.
I tried using your code (minus the group checking parts) and it worked fine. Are you getting any errors in the output?
For you to change the material too, your code should look something like this (added code where it says --CHANGE HERE:
DeBounce = false
Model = script.Parent.Parent
local lights = {}
local function FindLights(obj)
for _, child in pairs(obj:GetChildren()) do
if child:IsA("Light") then
table.insert(lights, child)
end
FindLights(child)
end
end
FindLights(game.Workspace["Light Switch"].Lights)
function OnClick(Player)
if script.Value.Value == 1 then
if Player:GetRankInGroup(4392500) >= 0 or Player:GetRankInGroup(2694395) >= 0 then
if DeBounce == false then
DeBounce = true
Model.Status.Sound:Play()
wait(1.3)
Model.Screen.BrickColor = BrickColor.new("Really black")
Model.Status.BrickColor = BrickColor.new("Really red")
for _, light in pairs(lights) do
light.Range = light.Range - 15
light.Parent.Material = Enum.Material.Concrete --CHANGE HERE
end
game.Lighting.FogEnd = game.Lighting.FogEnd - 100
game.Workspace["Light Switch"].PowerDown:Play()
Model.Exhaust.Smoke.Enabled = false
repeat Model.Body.Sound.Volume = Model.Body.Sound.Volume - .1
wait(.1)
until Model.Body.Sound.Volume <= 0
wait(1)
script.Value.Value = 2
DeBounce = false
else
end
else
end
elseif script.Value.Value == 2 then
if Player:GetRankInGroup(4392500) >= 0 or Player:GetRankInGroup(2694395) >= 0 then
if DeBounce == false then
DeBounce = true
Model.Status.Sound:Play()
wait(1.3)
Model.Screen.BrickColor = BrickColor.new("White")
Model.Status.BrickColor = BrickColor.new("Lime green")
for _, light in pairs(lights) do
light.Range = light.Range + 15
light.Parent.Material = Enum.Material.Neon --CHANGE HERE
end
game.Lighting.FogEnd = game.Lighting.FogEnd + 100
game.Workspace["Light Switch"].PowerUp:Play()
Model.Exhaust.Smoke.Enabled = true
repeat Model.Body.Sound.Volume = Model.Body.Sound.Volume + .1
wait(.1)
until Model.Body.Sound.Volume >= 1
wait(1)
script.Value.Value = 1
DeBounce = false
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(OnClick)
It depends, if there’s no performance or significant script/code-reducing reason for using CollectionService and the program’s working, there’s no great need to change it, but of course it’s always good to learn multiple ways to optimise a program.
It would be a good idea if more lights will be added in future under different models etc.
Another better option may be using :GetDescendants() rather than having a loop that loops through itself.