How Can I Make One Variable Equal Multiple Parts?

So I’m trying to make a function that turns on a group of lights. I plan to implement that function into another function that turns on each group of lights (6 groups), as well as changes the material of the part that the light is in to neon when it lands on a number in a countdown I made. The problem I’m having is that I’ll have to make a variable and an if statement for every single part, as well as every single light in those parts.

My question: Is there a way to make one variable be equal to multiple parts in the Workspace? All help is appreciated!

Here’s the code I have for one group of 4 lights that are under their own parts:

PreStageLight1 = script.Parent.Part1.PointLight
PreStagePart1 = script.Parent.Part1
PreStageLight2 = script.Parent.Part2.PointLight
PreStagePart2 = script.Parent.Part2
PreStageLight3 = script.Parent.Part3.PointLight
PreStagePart3 = script.Parent.Part3
PreStageLight4 = script.Parent.Part4.PointLight
PreStagePart4 = script.Parent.Part4

function PreStage()

PreStageLight1.Enabled = true 
PreStagePart1.Material = Enum.Material.Neon

PreStageLight2.Enabled = true 
PreStagePart2.Material = Enum.Material.Neon

PreStageLight3.Enabled = true 
PreStagePart3.Material = Enum.Material.Neon

PreStageLight4.Enabled = true 
PreStagePart4.Material = Enum.Material.Neon

PreStageLight4.Enabled = true 
PreStagePart4.Material = Enum.Material.Neon
	
end

PreStage()
1 Like

Use tables. Instance::GetChildren returns a table of all the children of Instance. So you can have all your lights in a folder then do

local lights = workspace.Lights:GetChildren()

for _, light in ipairs(lights) do
    --  Do something for every light
end
1 Like

Thanks so much! I’ll test that. What do you put after “for” by the way? And, when you do ipairs(lights), can you just make them all turn on at the same time? Like:

local lights = script.Parent.PreStageLights:GetChildren()

for _, light in ipairs(lights) do
lights.Enabled = true
end

Yeah that would iterate through all the lights inside of that table. And the _ is commonly used because sometimes you do not need to use the key or index and only want to use the value.

Also you need to do

light.Enabled = true

And not

lights.Enabled = true

Oh really? Even though the variable is named “lights”? Thanks.

You created a variable with the name(identifier) lights.
You assigned lights to the value of a table which contains all of the light objects in the folder by calling.

lights = script.Parent.PreStageLights:GetChildren()

lights.Enabled would not exist because lights is only a table containing those lights. You could do lights[1].Enabled because lights[1] would reference the first light object inside the table lights

So now you use a for loop to go through each light inside of the table you are referencing with the variable lights like so:

for _, light in pairs(lights) do
    light.Enabled = true
end
1 Like

Ohhh thanks so much for the explanation! I’ve just started scripting 2 weeks ago so I’m lacking sufficient knowledge on subjects like this haha.

Oh, there’s one problem with this. The pointlights are put under parts. How would I refer to the lights if there are 4 parts, and in each of those 4 parts there are 4 pointlights?

for _, lightPart in pairs(lights) do
    local light = lightPart:FindFirstChildOfClass("PointLight")
    if light then
        light.Enabled = true
    end
end
6 Likes

Hey thanks! It worked, but then the only problem is that when I put it into my Countdown script, it doesn’t do anything when I click my “Start Race” button. Maybe you can see where it went wrong? Here’s the script:

local seconds = script.Parent.Parent.Time
local text = script.Parent
local button = script.Parent.Parent.StartRace
lights = game.Workspace["Racing System"].RaceTimer3.StageSigns.PreStageLights.Lights:FindFirstChildOfClass("PointLight")
lightsparts = game.Workspace["Racing System"].RaceTimer3.StageSigns.PreStageLights.Lights:GetChildren()

script.Parent.Text = seconds.Value -- Makes the text in the TextLabel the same as what is in the "Time" Value under ScreenGui.

text.Visible = false

function PreStage()
for _, lightsparts in pairs(lightsparts) do
   
    if lights then
        lights.Enabled = true
    end
end

for _, lightsparts in pairs(lightsparts) do
    lightsparts.Material = Enum.Material.Neon
	end
end


local function Countdown() -- Makes the countdown code into a local function.

if seconds.Value == 5 then
	PreStage()
end

if seconds.Value == 5 then

end

for i = 1,seconds.Value do
 wait(1)
 seconds.Value = seconds.Value - 1
 script.Parent.Text = seconds.Value
end
-- Makes the TextLabel countdown from whatever number is in the "Time" Value under ScreenGui.

if seconds.Value == 0 then
	script.Parent.Text = "GO!"
end
-- When the TextButton value is 0, it becomes "GO!" instead of 0.

wait(1)

if script.Parent.Text == "GO!" then
	text.Visible = false
	end

end
-- Waits for two seconds, and then makes the TextLabel not visible.

button.MouseButton1Click:connect(function(play)
	text.Visible = true
	Countdown()
end)
-- Makes it so that when you hit the "StartRace" button, it makse the TextLabel visible and plays the "Countdown()" function.
1 Like