(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
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()
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 then
end
We need to fill in both of these things. The turning on bit it looks like you've already got:
if 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 comment