Lights turning on at night and off in morning help

Hello i know this is very very basic but im a very beginner scripter this is my code


If your wondering why im using a for loop its because i put every light part into a folder and trying to refrence it theres no errors but its not working.
Any info on what i got wrong would help!

1 Like

First of all, I think it would be a lot better for you to put the lights themselves in the for loop. Like:

for i, v in pairs(script.Parent.ParkinglotLightParts:GetChildren()) do

Then using v.LightPart.Spotlight

And for checking when the time hits the time you need, just check to see when the lightning value has changed, then use this for loop to change all the lights to on.

I know you’re a beginner and that might seem difficult but there are a lot of good videos on YouTube about for loops and I’d be happy to link one or try to explain more in-depth if you need me to.

1 Like

I would like to know more in depth because i want to learn scripting more and im sure that this will help with my project a lot!
Also is the v.lightpart.spotlight on another line down or the same line?

Of course. Here’s a really good video on for i, v loops. In Pairs Loops (i, v in pairs) - Roblox Beginner Scripting #18 - YouTube

This should help you better understand how to use these loops. They are super helpful and you will use them often, trust me they’ll be your friend.

Edit: the v.spotlight should be in the loop so after “do” and before “end”

1 Like

Clocktime is a write only mean that you cannot use if clock == 7,Instead use game.Lighting:GetMinutesAfterMidnight(),second,Referencing multiple same named item in a folder is a bad idea use a for loop to insert into a table instead.Thirdly,The for loop your using to turn on the light would finish running in a few frame so if the time change after it has finish running,It would not turn on or off

local timetoturnoff = 7 * 60 -- change the 7 to the hour after midnight the light should turn off
local timetoturnon = 18 * 60 -- change the 18 to the hour after midnight the light should turn on
while true do
wait(0.1)
    if game.Lighting:GetMinutesAfterMidnight() > timetoturnoff then
       --turn off light here
    end
    if game.Lighting:GetMinutesAfterMidnight() > timetoturnon then
      --turn on light here
    end
end

then you could use jacko for loop on top of the while true do loop

Like this?

I typically start a new line after “do” just for organization. As well as having the “end” on its own line as well.

So like,
for i, v in pairs(script.parent.parkinglotlightparts:GetChildren()) do

v.LightPart.Spotlight

end

1 Like

It gives me a error now but it says incomplete statement so im guessing i need to complete the script first for it to work.

Yes, that could be it. Once you watch that video I linked that should cover everything with for loops and give good examples. Then you can implement it as you need and how I’ve shown above as well as what @tin_nim has said.

And just for a note, as a beginner scripter Alvin Blox (the creator of the video I linked) has some really good beginner scripting tutorials and it’s a really good way to learn.

1 Like

Ok so i have the script wrote (i think) and it still gives me this error Yes1
This is the script

In the for loop, you aren’t doing anything to the object.

So like this it still gives me the error

It’s giving you the error because in the 6th line, you have wrote down the object but have not wrote anything for the object to do.

1 Like

I dont really know how to fix that…

Try this. (There may be some spelling mistakes)

local timetoturnoff = 7 * 60
local timetoturnon = 23*60

for _, v in pairs(script.Parent.parkinglotlightparts:GetChildren()) do
      while wait() do
              if game:GetService("Lighting"):GetMinutesAfterMidnight() > timetoturnoff then
                    v.LightPart.SpotLight.Enabled = false
                  else
                 v.LightPart.SpotLight.Enabled = true
             end 
      end
end
1 Like

Theres no errors but it still doesnt work.

You will need to place the for loop under an event that will fire when the time is updated. Otherwise it will never turn the lights on/off. So where ever you’re changing your time you could try there or using GetPropertyChangedSignal.

So maybe something like this to get the time to turn the lights on:

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
for _, v in pairs(script.Parent.parkinglotlightparts:GetChildren()) do
      while wait() do
              if game:GetService("Lighting"):GetMinutesAfterMidnight() > timetoturnoff then
                    v.LightPart.SpotLight.Enabled = false
                  else
                 v.LightPart.SpotLight.Enabled = true
             end 
      end
end

I dont know what im doing wrong it still wont work i think im gonna take a break and go to bed for the night scripting is hard for me even if its the easiest scripting language. Sorry if i wasted your time.

Keep learning,You’ll be great at script in the future here’s the full script

local timetoturnon = 7 * 60 --multiply the hour to minute
local timetoturnon = 23*60 --same

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
     if game.Lighting:GetMinutesAfterMidnight() > timetoturnoff and game.Lighting:GetMinutesAfterMidnight() < timetoturnon then
         --so If time is currently after 7 am and before 23 o'clock the light will be off
         for _, v in pairs(script.Parent.Parkinglotlightparts:GetChildren()) do
              v.LightPart.SpotLight.Enabled = false
         end
     else
             for _, v in pairs(script.Parent.Parkinglotlightparts:GetChildren()) do
                    v.LightPart.SpotLight.Enabled = false
             end
     end
end)
2 Likes

Ah that’s my bad @spydercam500. I forgot to take the while wait loop out after adding the value changed event. Thanks @tin_nim!

Like I said earlier, the channel from the video I linked has some great resources to learn more beginner scripting :slight_smile: