Roblox street lighting day and night script is broken for some reason

Some reason Roblox day night street lighting script is broken can anybody helps? help%20plase

3 Likes

The code in the picture you posted does everything it is supposed to do. What do you know about what it is not doing that you have not said? If you don’t have a part in workspace named LightPart, or the part doesn’t have a SpotLight named SpotLight in it, or the script isn’t in a container where scripts run, then this isn’t going to work how you expect it to. Otherwise, some more context is needed to explain what you’ve done wrong.

I personally wouldn’t recommend looping that very often, as that could cause unnecessary lag in your game. But in terms of syntax/API usage, I don’t see any errors in that script. If you could provide some screenshots of your console and explain what LightPart (and its children) is that would help us solve your issue.

I think you would be better off doing a loop that’s much more lax, say every 5-10 seconds, and then check if the time of day is greater than or less than a specific value, rather than comparing them directly with a fast loop.

1 Like

I might just not use this, but ask a friend if he can help.

You might want to help yourself learn by inserting prints in each functional path through your code so you understand what it is actually doing and then perhaps you can fix it yourself. :slight_smile:

I’m new so, i might need help and some how to understand these word.

Please answer these questions so we can help you

  • Show the object hierarchy (Take a screenshot if the game explorer)
  • What is your goal?
  • What errors are you getting?
  • What problems are you having?

It you provide a detailed reply, we can help you. If you so not, we can not help you.

2 Likes

https://gyazo.com/181f005013c786b608a144c0fb9d31f6
Making a street light script.
Non
Unknown problem

Hey, thanks for the information. I rigged up a nice repro for you to use. It works with multiple lights and has a ton of configuration. Let me know if you have any questions.

https://gyazo.com/eb8b1a176e2c0de2e41e9bafa5d6c8d0
This image was created using daySpeed 50.

The Problem

The current problem with your design was that you kept adding to minutesAftermidnight continually. You never reset it, causing it to break after the first night. Edit: SetMinutesAfterMidnight appears to auto correct the value you give it to a reasonable value. Still, the variable will never be compared correctly after that. Also, using the == operator prevented the original if statement from even runing. Your object getting failed to wait for the part to load, causing errors.

Object Hierarchy

How we organize the objects in the game

I used a model which holds the scripts and parts. The light parts hold a PointLight.
image

The script

This is the script that runs both the daylight cycle and the turning off and on of the lights.

I have added comments to try and explain everything in detail. However, if you still have questions, feel free to ask.

--Script in Model containing lights
local minutesAfterMidnight = 60*6 --Starts at 6 am
local daySpeed = 1 --Configure this to make days go shorter or faster (2 = twice as fast, 0.5 = half as fast)
local dayTime = 6 --Number of hours after midnight that it becomes day
local nightTime = 18 --Number of hours after midnight that it become night
local lightName = "Light" --Name of parts to store light in
local pointLightName = "PointLight" --Name of point light or other light object with .Enabled property
local lightDayColor = Color3.fromRGB(160, 160, 160) --Color to make light when day time
local lightNightColor = Color3.fromRGB(255, 255, 255) --Color to make light when night time

local lighting = game:GetService("Lighting") --Lighting service
local model = script.Parent --Where are lights are stored
while true do
	minutesAfterMidnight = minutesAfterMidnight + (daySpeed) --Increments counter based on daySpeed
	if minutesAfterMidnight > 60*24 then --Resets counter at midnight
		minutesAfterMidnight = 0
	end
	
	lighting:SetMinutesAfterMidnight(minutesAfterMidnight) --Sets time
	
	if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then --Between 6am and 6pm (day)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.SmoothPlastic --Set material
				light[pointLightName].Enabled = false --Disable light
			end
		end
	else --Between 6pm and 6am (night)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.Neon --Set Material
				light[pointLightName].Enabled = true --Enable light
			end
		end
	end
	
	wait()
end

Download and Summary

While I would love if you followed along, feel free to take the repro below. It will work instantly without any additional configuration.

Hope I could help you make a successful day/night cycle with street lamps.
Note: The street lamps are not well made and are simply two parts. I am not a good builder so I advise you make your own based off of them.

Roblox Model:

Direct Download:
StreetLights Repro.rbxl (16.2 KB)

8 Likes

Thanks, appreciate you’re help.

If that solved your problem, mark it as the solution using the checkbox to the left of the like button.

I ran the exact script and it worked. It reads from GetMinutesAfterMidnight() and not just minutesAfterMidnight, so this problem actually won’t happen.

Huh, it appears SetMinutesFromMidnight auto fixes the value. It prevents it from overflowing to a non-existant time.

Still, checking for the exact value using == would not work. I believe the model hierarchy was just not correct.

its works but the issue is it randomly turns on and off