I added a night/day cycle script to my game, and it is working very good. So, I decided to add a light column (that works with SpotLight). Then I added a script to the SpotLight, this script is supposed to make the spot light opens in night(19:00:00) and close after 600 seconds(5:00:00). But the script is not working, here is the script:-
Lighting.Parent.TimeOfDay:connect(function(Time) if TimeOfDay = 19 then SpotLight = true while wait(600) SpotLight = false end end
Here is the day/night cycle script if you want to know if it is wrong, note that I don't have troubles with this script:-
local dayLength = 24 local cycleTime = dayLength*60 local minutesInADay = 24*60 local lighting = game:GetService("Lighting") local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime local endTime = startTime + cycleTime local timeRatio = minutesInADay / cycleTime if dayLength == 0 then dayLength = 1 end repeat local currentTime = tick() if currentTime > endTime then startTime = endTime endTime = startTime + cycleTime end lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio) wait(1/15) until false
NOTE: I am new to scripting, so you will find lots of mistakes.
Thanks for helping.
In line 1 of your spotlight script, you connect to Lighting.Parent.TimeOfDay
, but no event. You might want to try Lighting.changed:connect(
, etc. In line two, you access the TimeOfDay property. That property is actually a string, so to compare, you should check if TimeOfDay equals '19:00:00'
. A full spotlight script is provided below with other enhancements.
game.Lighting.changed:connect(function() --connect when a lighting property changes local myTime = game.Lighting.TimeOfDay --set myTime equal to lighting's TimeOfDay if myTime == '19:00:00' then --if its 1900 hours then SpotLight.enabled = true --enable the spotlight. Double check me if enabled is capitalized or not. else if myTime == '5:00:00' --if it's not 1900 then if it's 0500 then SpotLight.enabled = false --disable the spotlight end --close the if statement end) --close the connect line
I will add a 'while true do', since i know best using this one. So, add a while true do in a new script:
while true do wait() end
Okay, now let's make the part of disable and enable.
if game.Lighting.TimeOfDay == 19 then SplotLight.Enabled = true end if game.Lighting.TimeOfDay == 5 then SpotLight.Enabled = false end
Try that, reply if it doesn't work and tell me what you see in output