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