Hello Developers, I am having some trouble with my script, I’d love it if you could take some time out of your day to help a fellow noob scripter!
What do you want to achieve?
I want to create a script that updates the material and lighting of streetlights in my Roblox game based on the in-game time of day.
What is the issue?
The script I have written does not seem to update the streetlights as expected. The lights do not change materials or toggle on/off based on the time of day. I have included my current script below:
local function updateStreetlights()
local timeOfDay = game.Lighting.ClockTime
local dayStart = 6 + 25/60
local nightStart = 17 + 45/60
local isDaytime = timeOfDay >= dayStart and timeOfDay < nightStart
local streetlightModels = {"YUP1", "YUP2", "YUP3", "YUP4", "YUPoutside", "YUPoutside2"}
for _, modelName in ipairs(streetlightModels) do
for _, model in pairs(workspace:GetDescendants()) do
if model:IsA("Model") and model.Name == modelName then
local lightGroup = model:FindFirstChild("Light")
if lightGroup then
local bulbParts = lightGroup:FindFirstChild("BulbParts")
if bulbParts then
for _, lightPart in pairs(bulbParts:GetChildren()) do
if lightPart:IsA("BasePart") and lightPart.Name == "LightPart" then
lightPart.Material = isDaytime and Enum.Material.Metal or Enum.Material.Neon
end
end
end
local mainGroup = lightGroup:FindFirstChild("Main")
if mainGroup then
local lightAttachment = mainGroup:FindFirstChild("LightAttachment")
if lightAttachment then
local light = lightAttachment:FindFirstChild("Light")
if light and (light:IsA("SpotLight") or light:IsA("PointLight") or light:IsA("SurfaceLight")) then
light.Enabled = not isDaytime
end
end
end
end
end
end
end
end
updateStreetlights()
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(updateStreetlights)
What solutions have you tried so far?
I have looked through the Developer Hub and tried different variations of the script to see if I could identify the issue. I also checked that the paths to the light parts and attachments are correct.
Additional details:
The streetlights are models named “YUP1”, “YUP2”, “YUP3”, “YUP4”, “YUPoutside”, and “YUPoutside2” in the workspace.
Each streetlight model contains a “Light” group with “BulbParts” and a “Main” group with “LightAttachment”.
The script should change the material of “LightPart” to Neon at night and Metal during the day, and toggle the lights on/off accordingly.
Please help me identify what might be going wrong with my script and how I can fix it.
Your code is very poorly made in terms of readability and also has a pointless loop. You don’t need to loop through the list of strings streetlightModels you can just use table.find()
You should really space your code out.
Example:
local names = {"part1", "part2"}
for _, part in pairs(game:GetService("Workspace"):GetChildren()) do
if part.ClassName == "Part" then
local found = table.find(names, part.Name)
if found then
print(part.Name, "in table names")
else
print("ignoring", part.Name)
end
else
continue
end
end
Output:
22:56:32.699 ignoring Baseplate - Server - Script:13
22:56:32.700 part1 in table names - Server - Script:11
22:56:32.700 part2 in table names - Server - Script:11
22:56:32.700 ignoring part3 - Server - Script:13
Make a folder called Poles on the workspace and put them all in.
I think that is it… Little shaky on that toggle but that should fit most day time starts.
local dayStart = 6.5 -- 6:30 AM
local nightStart = 17.75 -- 5:45 PM
local lightsFolder = workspace:WaitForChild("Poles")
local toggle = game.Lighting.ClockTime >= nightStart or game.Lighting.ClockTime < dayStart
while true do
local timeOfDay = game.Lighting.ClockTime >= nightStart or game.Lighting.ClockTime < dayStart
if toggle ~= timeOfDay then toggle = timeOfDay
for _, pole in pairs(lightsFolder:GetChildren()) do
local light = pole.Light.Main.Attachment.Light
light.Enabled = timeOfDay
end
end task.wait(8)
end
With Neon
local dayStart = 6.5 -- 6:30 AM
local nightStart = 17.75 -- 5:45 PM
local lightsFolder = workspace:WaitForChild("Poles")
local toggle = game.Lighting.ClockTime >= nightStart or game.Lighting.ClockTime < dayStart
while true do
local timeOfDay = game.Lighting.ClockTime >= nightStart or game.Lighting.ClockTime < dayStart
local material = timeOfDay and Enum.Material.Neon or Enum.Material.Glass
if toggle ~= timeOfDay then toggle = timeOfDay
for _, pole in pairs(lightsFolder:GetChildren()) do
local light = pole.Light.Main.Attachment.Light
light.Enabled = timeOfDay
local bulbParts = pole.Light.BulbParts
for _, lightPart in pairs(bulbParts:GetChildren()) do
lightPart.Material = material
end
end
end
task.wait(8)
end
looks a bit pushy but as long as everything is in the same spot in the poles, I’m pretty sure everything will be there by the time you first turn these on.
My game starts off at 7 am, so the streetlights should be set to metal and the light should be off, and then set to neon and enable the light at 5:45 PM
You sent me a streetlight. The attachment in that was way off and needed to be reset back to the box it was ment to be in. I just deleted it and and created a new one in that Main.
What the pole name is will not matter; Outsidepole (or whatever it was named). In the for loop that will end up as the _, pole found by GetChildren… It then followes the path to the light to switch it on and off …
local light = pole.Light.Main.Attachment.Light ← as long as each pole has this same set up it should be fine. I did test this in studio and it was working well and very light on taking up task time.
So the poles named “Outsidepole” exc… all go in the folder put on the workspace;
local lightsFolder = workspace:WaitForChild(“Poles”). It should only access them two times a cycle, on and off (true or false) from the outcome of timeOfDay…
local timeOfDay = game.Lighting.ClockTime >= nightStart or game.Lighting.ClockTime < dayStart
if that value changes toggle let’s it pass for the switch.
Also this is a ServerScript in ServerScriptService and it will do every streetlight you have…
So they all need to be in that folder by whatever name they are.
This may seem complicated but, it is not, we are just checking the time then changing light.Enabled on the poles in the poles folder; timeOfDay = game.Lighting.ClockTime >= nightStart… if it’s night this is true.
I am purposely trying to be as light on the tasks as possible. This is just a part of the game not the main task. There will be a delay in the switch… It will not switch perfectly on the time stated.
This is set up to start with the lights off (false) and the time the game starts on needs to be lower than the nightStart and higher than dayStart.