So I have this script that is supposed to turn on all the streetlights in my game. All the streetlights are a put in a folder called "StreetLights" and have a part named "Light" with a spotlight in it.
My script attempts to cycle through the streetlights folder and turn them all on or off depending on the time, however it fails to do so.
minutesAfterMidnight = 7 * 60 while wait(0.1) do minutesAfterMidnight = minutesAfterMidnight + 10 game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight) end function streetLights() if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then local streetlights = game.Workspace.Map.StreetLights:GetChildren() for i = 1, #streetlights do if streetlights[i]:FindFirstChild("Light") ~= nil then streetlights[i].Light.Material = "SmoothPlastic" streetlights[i].Light.SpotLight.Enabled = false end end end if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then local streetlights = game.Workspace.Map.StreetLights:GetChildren() for i = 1, #streetlights do if streetlights[i]:FindFirstChild("Light") ~= nil then streetlights[i].Light.Material = "Neon" streetlights[i].Light.SpotLight.Enabled = true end end end end game.Lighting.Changed:connect(streetLights)
This is how you structured your Parents and Children, so make sure yours looks the same (The folders can be parts or models or whatever though).
-- Make your variables local unless they have to be global. local minutesAfterMidnight = 7 * 60 -- Define things now so that you don't have to repeat yourself (DRY) -- Utilize WaitForChild() on children, else it'll error online. local lighting = game:GetService("Lighting") local map = workspace:WaitForChild('Map') local StreetLights_folder = map:WaitForChild("StreetLights") local all_streetlights = StreetLights_folder:GetChildren() -- Use one function for both local function switch(on_off) for i = 1, #all_streetlights do local light = all_streetlights[i]:FindFirstChild("Light") if light then local spot_light = light:findFirstChild('SpotLight') if spot_light then spot_light.Enabled = on_off end light.Material = on_off and "Neon" or "SmoothPlastic" end end end function streetLights() if lighting:GetMinutesAfterMidnight() == 6 * 60 then switch(false) elseif lighting:GetMinutesAfterMidnight() == 18 * 60 then switch(true) end end -- Use 'C'onnect, 'c'onnect is deprecated lighting.Changed:Connect(streetLights) -- Any loop will yield/wait until it has finished or been broken -- Put this at the bottom while wait(0.1) do minutesAfterMidnight = minutesAfterMidnight + 10 lighting:SetMinutesAfterMidnight(minutesAfterMidnight) end