Lights turning on at night and off in morning help

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:

Ok theres no errors in the script that i see but when i go to test it it doesnt work and says this

LightPart is not a valid member of Part
Workspace.Map.TopSide.ParkingLot.ParkingLotLightParts.LightPart
@Jacko_Korbius
@tin_nim

Can you show the explorer tab of your folder, the parts, and the light object itself?

1 Like

Yes1
It goes map then topside then parking lot then parking lot light parts then the parts then the spot lights.

Where is your script located? And are all the light parts under “ParkinglotLightParts”?

1 Like

Script is located in the Parking lot folder not the light part folder.

Okay so edit your script to this:

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.SpotLight.Enabled = false
         end
     else
             for _, v in pairs(script.Parent.Parkinglotlightparts:GetChildren()) do
                    v.SpotLight.Enabled = false
             end
     end
end)

Where you see “v.SpotLight.Enabled” is where the error was, you didn’t need to have “v.LightPart.Spotlight”, so we just took out “LightPart” since v is LightPart.

Edit: Not sure why my formatting isn’t working for the code but copy from the first “local” to the last “end)” Edit2: Never mind, got it to work.

2 Likes

Yes!! It finally works i just changed the v.lightpart.spotlight to just v.spotlight and it worked! Thank you so much!
Also this is irrelevant but how did u learn to script?

Hooray!

Don’t want to get too off topic here but I learned using the Roblox API Documentation as well as Youtube videos for beginning scripting. This is a pretty good playlist: Alvin Blox’s beginner scripting tutorials. This is what I used a lot, and there is an advanced tutorial on his channel as well for once you finish the beginner series: TheDevKing’s beginner scripting series.

I wish you the best of luck on learning, have a good one!

3 Likes

How is your scripting journey coming along?