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

Would someone help me fix my automatically opening and closing door?

Asked by 6 years ago

I'm trying to make a door that opens at a certain time but my code won't work. I've gotten it to work a couple of times but it'll just open once and close once and not loop. Any help is appreciated.

minutesAfterMidnight = 0
while true do
minutesAfterMidnight = minutesAfterMidnight + 60
game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
wait(.1)
end

while true do
if minutesAfterMidnight == 360 then

    script.Parent.Transparency = 1

    script.Parent.CanCollide = false

    elseif minutesAfterMidnight == 720 then

    script.Parent.Transparency = 0

    script.Parent.CanCollide = true
wait(.1)
    end
end

1 answer

Log in to vote
0
Answered by 6 years ago

It isn't working because you have 2 while true do loops. The first one never exits/breaks, so the second one never gets to start. Instead, combine them:

minutesAfterMidnight = 0
while true do
    minutesAfterMidnight = (minutesAfterMidnight + 60) % 1440
    game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
    if minutesAfterMidnight == 360 then
        script.Parent.Transparency = 1
        script.Parent.CanCollide = false
    elseif minutesAfterMidnight == 720 then
        script.Parent.Transparency = 0
        script.Parent.CanCollide = true
    end
    wait(.1)
end

The (minutesAfterMidnight + 60) % 1440 means that once minutesAfterMidnight is about to become 1440, it's automatically changed to 0 (since % 1440 means "the remainder after dividing by 1440"), allowing your door to open/close every day rather than just the first day.

0
Thank you so much! bretinat0r 4 — 6y
Ad

Answer this question