So I made this Day/Night script:
function Timer() wait(0.1) game.Lighting.TimeOfDay = TimeOfDay + 2 end while wait(0.1)do Timer() end
This didn't work! can someone tell me why?
You haven't defined TimeOfDay
(used on the right side of the =
in line 3).
Lua won't guess what you mean. You have to be explicit and unambiguous:
game.Lighting.TimeOfDay = game.Lighting.TimeOfDay + 2;
However according to the Wiki article on TimeOfDay
, says TimeOfDay
is a string (text), which means you cannot add 2
to it.
If we look at the wiki article on Lighting, we can see there is a number GetMinutesAfterMidnight()
method and a SetMinutesAfterMidnight(number minutes)
method. We have to use those instead:
game.Lighting:SetMinutesAfterMidnight( game.Lighting:GetMinutesAfterMidnight() + 2 )
The wait(0.1)
on line 2 of your snippet is probably frivolous. You already have a wait on line 5. If you want the interval to be ever 0.2
seconds, you should just edit the one in the while
loop to use that amount.
TimeOfDay is set in this form: num:num:num. For this reason, you cannot add 2. Second of all, you would do game.Lighting.TimeOfDay = game.Lighting.TimeOfDay + 2
, not game.Lighting.TimeOfDay = TimeOfDay + 2
because you're saying to add 2 from the original time. This is the correct way to do it:
speed = 1 sec = 0 mins = 0 hour = 0 while true do game.Lighting.TimeOfDay = ""..hour..":"..mins..":"..sec.."" sec = sec + (5*speed) if sec >= 60 then sec = 0 mins = mins+1 end if mins >= 60 then mins = 0 hour = hour + 1 end if hour >= 24 then hour = 0 end wait(0.05) end
This basically adds one second to the time every 0.05 seconds. I hope this helped!