Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What do I use to find out if the TimeOfDay has changed? {UNANSWERED}

Asked by
Prioxis 673 Moderation Voter
10 years ago

I'm making a script whenever it reaches a certain time in-game like 18:00:00 it will turn the spotlight on and when it is 07:00:00 it'll turn off

here's my script so far

local l = game.Lighting
while true do
    if game.Lighting.TimeOfDay = 18:00:00 then
        script.Parent.SpotLight.Enabled = true
    elseif
        game.Lighting.TimeOfDay = 07:00:00 then
        script.Parent.SpotLight.Enabled = false
wait()  
    end
end
end
0
1. PUT A WAIT OR IT'LL CRASH fireboltofdeath 635 — 10y

2 answers

Log in to vote
2
Answered by
TaslemGuy 211 Moderation Voter
10 years ago

(EDIT to fix typo, L should have been : in several cases)

Your script has a lot of issues. We'll rebuild it from scratch.

We want a loop that's constantly running. So we'll make a loop like this:

while true do
    -- something goes here
end

There's a problem, though: if you run this, it will just crash. So we need to put a wait inside:

while true do
    wait()
    -- something goes here
end

Great! Next, we need to check whether the current time is 18:00:00. How can we do this? For now, forget about the loop.

if --[[ it's currently 18:00:00 ]] then
    -- turn on the light
end

We need to fill in both of these things. The turning on bit it looks like you've already got:

if --[[ it's currently 18:00:00 ]] then
    script.Parent.SpotLight.Enabled = true
end

So now we've just got to figure out that condition. The time is stored as a string or a sequence of characters. So what we want to check is string equality.

We're comparing two things. The first is the string "18:00:00" and the second is the current time, game.Lighting.TimeOfDay. To compare two things, you use the == operator. So we want to write:

game.Lighting.TimeOfDay == "18:00:00"

There's a problem, though, which you might experience. Basically, the problem is that your script could potentially miss the exact time of 18:00:00. Depending on how fast your time-advancing script runs, you might skip forward. So what we can check instead is whether it's the 18:yy:yy without caring about anything other than the current hour.

To do this, we'll use :sub. :sub is a string method which returns substrings or pieces of strings. We'll look at the first 3 characters of the time of day only.

We write game.Lighting.TimeOfDay:sub(1, 3) which gets characters 1-3 from the string. So we want it to be "18:" instead of the full string. So we'll say:

game.Lighting.TimeOfDay:sub(1,3) == "18:"

We'll put this into the if-statement:

if game.Lighting.TimeOfDay:sub(1,3) == "18:" then
    script.Parent.SpotLight.Enabled = true
end

And now we can put this into the while-loop:

while true do
    wait()
    if game.Lighting.TimeOfDay:sub(1,3) == "18:" then
        script.Parent.SpotLight.Enabled = true
    end
end

And we repeat for the other time, turning off the lights:

while true do
    wait()
    if game.Lighting.TimeOfDay:sub(1,3) == "18:" then
        script.Parent.SpotLight.Enabled = true
    end
    if game.Lighting.TimeOfDay:sub(1,3) == "07:" then
        script.Parent.SpotLight.Enabled = false
    end
end

And there you have it!

1
OMG thank you so much I''ve never had a single person on this entire site explain something as good as you did :D Prioxis 673 — 10y
1
it didn't work Prioxis 673 — 10y
1
I see why it didn't work, he used 'L' instead of the ':'. XD TheeDeathCaster 2368 — 10y
0
Oops. That's a bit mistake. TaslemGuy 211 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Try this;

while wait(0)do --Waits 0 seconds non-stop
if game.Lighting.TimeOfDay == "18:00:00"then --If its 18 o'clock
script.Parent.SpotLight.Enabled = true --Enables
elseif game.Lighting.TimeOfDay == "07:00:00"then --Elseif its 7 o'clock
script.Parent.SpotLight.Enabled = false --Disables
end end --all the ends (For elseif doesn't require a end)

I hope this helped!

Answer this question