local lighting = game:GetService('Lighting')
minutesAfterMidnight = 3*60
function streetLights()
for _, v in pairs(workspace:GetChildren()) do
if v.Name == 'Streetlight' then
local light = v:FindFirstChild('Light')
if light then
return light
end
end
end
end
while wait(0.1) do
minutesAfterMidnight = minutesAfterMidnight + 10
lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
if lighting:GetMinutesAfterMidnight() == 6 * 60 then
streetLights().Material = 'SmoothPlastic'
streetLights().SpotLight.Enabled = false
end
if lighting:GetMinutesAfterMidnight() == 18 * 60 then
streetLights().Material = 'Neon'
streetLights().SpotLight.Enabled = true
end
end
Not sure if this is the most efficient method, but either way, the lights aren’t being changed
When a value is returned from a function, this exits execution immediately. As such, you are changing the properties of just a single part if it is able to be found.
Yep, as Avigant said, return will end the function immediately, so it will only return 1 light part. You should make the function change the material and light enabled.
function ChangeStreetLights(material, SpotLightEnabled)
for _, v in pairs(workspace:GetChildren()) do
if v.Name == 'Streetlight' then
local light = v:FindFirstChild('Light')
if light then
light.Material = material
light.SpotLight.Enabled = SpotLightEnabled
end
end
end
end
ChangeStreetLights('SmoothPlastic', false)
ChangeStreetLights('Neon', true)
As there are many other things in workspace, I would suggest you to store all lights in a table first so it need less time to scan the workspace again.
local LightTable = {}
for _, v in pairs(workspace:GetChildren()) do
if v.Name == 'Streetlight' then
local light = v:FindFirstChild('Light')
if light then
LightTable[#LightTable+1] = light
end
end
end
I wouldn’t really say it’s more optimised to use CollectionService; all you’re doing is changing the array that you’re fetching from. Even if there is difference in time, it’d be fairly negligible.
CollectionService would surely be an simple way to mark light parts and just go through it when you need to interact with it, especially if they don’t share the exact same ancestry and there are lights to be interacted with elsewhere in your object hierarchy.
local lighting = game:GetService("Lighting")
local day, night, speed = 6*60, 18*60, 10
function toggleStreetlights(on)
for _, streetlight in next, workspace:GetChildren() do
if streetlight.Name == "Streetlight" and streetlight:FindFirstChild("Light") then
streetlight.Light.Material = on and 'Neon' or 'SmoothPlastic'
streetlight.Light.SpotLight.Enabled = on
end
end
end
while wait(0.1) do
lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + speed)
if lighting:GetMinutesAfterMidnight() == day then
toggleStreetlights(false)
elseif lighting:GetMinutesAfterMidnight() == night then
toggleStreetlights(true)
end
end
If all the streetlights are direct children of the workspace, as the original code implies, just put them all in a Folder or Model, then you don’t have to iterate over all the workspace children at all, just the folder children.