Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.