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!
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.
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?
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
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.
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
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)