I am currently trying to finish up a script that simply controls a set of streetlights to come on during the night then turn of during the day. I currently have it mostly working however I have a few places where two light parts have to exist alongside one another but when the script runs only the lights inside one of those parts gets enabled and the other remains disabled if I try naming one LightPart and one LightPart2 i get the error LightPart2 is not a valid member of model.
script being used
while true do
wait(5)
if game.Lighting:GetMinutesAfterMidnight() > 16 * 60 then ------ Change 16 to any number you wish (16 = 4:00PM)
for i, v in pairs(game.Workspace.Streetlights:GetChildren()) do
if v.Name == "Streetlight" then
v.LightPartThing.Material = "Neon"
v.LightPartThing.SpotLight.Enabled = true
else
print("nothing")
end
end
elseif game.lighting:GetMinutesAfterMidnight() > 8 * 60 then ------ Change 8 to any number you wish (8 = 8:00AM)
for i, v in pairs(game.Workspace.Streetlights:GetChildren()) do
if v.Name == "Streetlight" then
v.LightPartThing.Material = "SmoothPlastic"
v.LightPartThing.SpotLight.Enabled = false
end
end
end
end
The above is how everything is layed out but as mentioned above only one SpotLight will actually get enabled the other will get ignored.
Because of this layout, you need a more complex function that can call itself (is recursive). It should be initially called on Streetlights, it should loop over each child: if the child is a Model or Folder, the same function should be called on that; otherwise, if it is a LightPartThing, do your intended changes then return.
Curious what type of object are your spotllights in? I don’t recognize that symbol. You should be able to access them if you changed one of their names. You stated above you called it LightPart2 but in your script you are calling things LightPartThing so just make sure the names and the parts are correct. If you rename one of them you can also use FindFirstChild instead of the dot.
You need to go through the children of each Streetlight model and handle for each LightPartThing rather than for one per model. Instead of cycling through the child twice, I will use GetDescendants to check for any parts named LightPartThing.
Also, you can condense the wait(5) and the while true do into one line (and use task.wait not wait, it’s better). Additionally, you should use ipairs for this case not pairs.
One last nitpick, it’s better to call a service once at the start than to call it over and over.
local Lighting = game:GetService'Lighting'
while task.wait(5) do
if Lighting:GetMinutesAfterMidnight() > 16 * 60 then ------ Change 16 to any number you wish (16 = 4:00PM)
for i, v in ipairs(workspace.Streetlights:GetDescendants()) do
if v.Name == "LightPartThing" then
v.Material = "Neon"
v.SpotLight.Enabled = true
end
end
elseif Lighting:GetMinutesAfterMidnight() > 8 * 60 then ------ Change 8 to any number you wish (8 = 8:00AM)
for i, v in ipairs(workspace.Streetlights:GetDescendants()) do
if v.Name == "LightPartThing" then
v.Material = "SmoothPlastic"
v.SpotLight.Enabled = false
end
end
end
end