Electric Generators

Hello, I just needed help with a script.

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)
2 Likes

Remember to format scripts with this (with 3 ` before and after), then we can copy it properly and read it more clearly to understand it

1 Like

Oh I didn’t know, I will do it for the next time :raised_hands:

2 Likes

So you want them to be neon when on and concrete when off? Are there any errors or do you not know how to?
You can use

part.Material = Enum.Material.Concrete or "Concrete" to do this.

I just don’t know how to and when I thought I did, the generators didn’t work. I can provide videos of how these work and what do I refer to.

1 Like

Yes do provide the videos, and what you used to attempt to change the material, but try this first

For the Power up, insert this line which changes the material:
part.Material = Enum.Material.Neon

and power down, this line:
part.Material = Enum.Material.Concrete

Okay i’ll put this code below light.Range = light.Range - 15 and light.Range = light.Range + 15

This is how it works with the script not modified:

And this is how it works with the lines you provided me.

I also tried to use scripts such as:

local function ChangeMaterialForModel(model, newMaterial)
local parts = {}

but they have ended up wrong

1 Like

Is it working well then?

For something like this:

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:
image
Before:

After:

If you want, I can provide example for this with lights too.

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

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.

1 Like

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)
1 Like

Yeah!
No errors in the output, everything works perfectly now.

Thank you so much man!

1 Like

Wouldn’t it be easier to use CollectionService for all of the lights instead of just looping through a model?

2 Likes

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.

1 Like

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