You can write your topic however you want, but you need to answer these questions:
I tried making a script for a part that only emits lights between the evening and morning, but never at the daytime.
However, while the script seems to work in studio, the lights remain on during the day when loaded ingame even when I try updating the time of the day, only difference is that the script is stored in ReplicatedStorage, which is absolutely necessary for me.
I couldn’t find any solutions in the devforum, and the script used to work flawlessly months ago, so an update must’ve broke the script.
while true do
wait(1)
if game.Lighting.TimeOfDay >= "18:00:00" then
script.Parent.Light.Enabled = true
elseif game.Lighting.TimeOfDay >= "07:00:00" then
script.Parent.Light.Enabled = false
end
end
oh ur if and elseif statements are the reason why its not working. try this, it may not work but hopefully it does… also task.wait is usually better than wait just a heads up. (-:
while true do
task.wait(1)
local TimeOfDay = game.Lighting.TimeOfDay
if TimeOfDay >= "18:00:00" or TimeOfDay < "07:00:00" then
script.Parent.Light.Enabled = true
else
script.Parent.Light.Enabled = false
end
end
local Lighting = game:GetService("Lighting")
while true do
local CurrentTime = Lighting:GetMinutesAfterMidnight() / 60
if CurrentTime >= 18 then
script.Parent.Light.Enabled = true
elseif CurrentTime >= 7 then
script.Parent.Light.Enabled = false
end
wait(1)
end
Woke up thinking about my last reply. This one is way better.
5 seconds stall isn’t going to matter but will make this less cycle-greedy.
This one also checks to see if it needs to switch the light or not, instead of always updating.
(in case you ever wanted to do a folder with many lights, with one script)
local light = script.Parent.Light
local last
while task.wait(5) do
local t = game.Lighting.ClockTime
local switch = t >= 18 or t < 7
if switch ~= last then
light.Enabled = switch
last = switch
end
end
Also, none of these scripts will work out unless you have a script moving the time along…
Time isn’t moving on it’s own by default.
Use the ClockTime property. TimeOfDay is a string value. (I use a translator, there may be errors in my message). Here’s the code, it works.
while true do
wait(1)
if game.Lighting.ClockTime >= 18 then
script.Parent.Light.Enabled = true
elseif game.Lighting.ClockTime >= 7 then
script.Parent.Light.Enabled = false
end
end
Did not fix it unfortunately, I have a script that allows me to change time manually, so don’t worry about that. I should note that I spawn the parts from Replicated Storage.
Mine is set up a bit odd because it’s like one step away from running all the lights.
If this is just one light you don’t need the last and switch logic, like this;
local light = script.Parent.Light
while task.wait(5) do
local t = game.Lighting.ClockTime
local mode = t >= 18 or t < 7
light.Enabled = mode
end