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

Script doesn't work?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I wanted to make a simple script that would turn on a light after and before a certain time, but it doesn't work.

if
    game.Lighting.TimeOfDay >= "17:40:00" or <= "06:10:00"
then
    script.Parent.Enabled = true
else
    script.Parent.Enabled = false
end

2 answers

Log in to vote
3
Answered by
Discern 1007 Moderation Voter
10 years ago

So you need a while loop to continually check to see if the time is correct. Also, you are checking if the value is greater or less than a StringValue, as opposed to a numerical value or integer. This will result in an error, you must use a numerical value or integer when using greater than or less then. Also, when you use "or" or "and", you need to restate the entire statement.

To the get the numerical values of the minutes in Lighting, you would use the :GetMinutesAfterMidnight() method. This returns the amount of minutes that has passed after midnight. The minutes after midnight in 17:40:00 is 1060, and the minutes after midnight in 6:10:00 is 370.

Check this out:

while true do --Creates the while loop.
    local minutes = game.Lighting:GetMinutesAfterMidnight() --Returns the amount of minutes after midnight (00:00:00).
    if minutes >= 1060 or minutes <= 370 then --Checks if the time is correct.
        script.Parent.Enabled = true
    else
        script.Parent.Enabled = false
    end
    wait(.1) --Essential so the script doesn't crash.
end

NOTE: Script tested and works.

Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
10 years ago

It's because you're telling the script to enable the light if the time is after or before a string value that won't work because you can only compare quantity in numbers. So, we need to take away the quotes and replace them like this:

while true do --Loops every 1 second to check if conditions are met
    wait(1)
    X=game.Lighting.TimeOfDay:sub(1,2) --Finds hours
    X=X+0
    Y=game.Lighting.TimeOfDay:sub(4,5) --Finds minutes
    Y=Y+0
    if
        X>=17 and Y>=40 then --Turns on if hours are past 17 and minutes past 40
        script.Parent.Enabled = true
    elseif X>=6 and Y>=10 then --Turns off if hours are past 6 and minutes past 10
        script.Parent.Enabled = false
    end
end

Hope I helped! If you have any questions, please leave a comment :P

Answer this question