How do you make a door open after X amount of time has passed?

Hello!
I would like to make a door object script “open” (disappear with no animation nor collisions) for let’s say 1-hour ingame, after that, it will open for 10 seconds for the player to go thru the door and go to the next area, if the player doesn’t go thru in those 10 seconds the door closes back, resetting the timer to 1 hour again.

When the door reaches 1 hour in game time, it opens for 10 seconds for THAT person to go thru if he doesn’t go thru the door, it closes back again, and reset the loop to 1 hour and the loop “loops again”.
The player is free to stay in the beggining area without passing thru that door, the loop will restart regardless after the 10 seconds delay if he pass thru or not.

1 Like

For the door touched part, this should be achievable by using a touched event on the door. You can delay a function for 10 seconds and if the door was not touched disconnect the touched event. Something like this:

local DoorConnection = nil;
local isDoorTouched = false

delay(10,function()
DoorConnection:Disconnect()
if not isDoorTouched  then
warn("the door was not touched")
-- close the door
end
end)

DoorConnection = Door["Touched"]:Connect(function(Obj)
isDoorTouched = true
end)

For the waiting an hour part, I suggest a while loop and have the wait at 3600 seconds.

Thanks , i’m not that good at code, how can i add that loop to make it do that?

1 Like

You can do the following
[I’m on mobile so this may take a bit to type]
[May also be errors]

local isDoorOpened = false
local DoorTouched = false
local DoorConnection = nil
while wait(3600) do
--open door
isDoorOpened = true

delay(10,function()
isDoorOpened = false
DoorConnection:Disconnect()
if not isDoorTouched then
warn("not touched")
end
end)

DoorConnection = Door["Touched"]:Connect(function()
DoorTouched = true
end)

repeat wait() until isDoorOpened = false
-- close door
end

I added the script to a part for a test.

local isDoorOpened = false
local DoorTouched = false
local DoorConnection = nil
while wait(5) do
	--open door
	isDoorOpened = true

	delay(10,function()
		isDoorOpened = false
		DoorConnection:Disconnect()
		if not isDoorTouched then
			warn("not touched")
		end
	end)

	DoorConnection = Door["Touched"]:Connect(function()
		DoorTouched = true
	end)

	repeat wait() until isDoorOpened = false
	-- close door
end

i changed the wait time to 5 to see if it disappear or not, the part don’t seem to do anything.

So from my understanding. It loops through an hour, everytime that hour ends, there will be a 10 second wait. If somebody goes in within the 10 seconds, the loop stops; but if nobody goes in, the timer is resetted to an hour?

I don’t think you are actually doing anything to do the door. There is just timings and logic

1 Like

When the door reaches 1 hour in game time, it opens for 10 seconds for THAT person to go thru if he doesn’t go thru the door, it closes back again, and reset the loop to 1 hour and the loop “loops again”.
The player is free to stay in the beggining area without passing thru that door, the loop will restart regardless after the 10 seconds delay if he pass thru or not.

1 Like

Just a tip, use the task library.

For this scenario, use task.delay instead of delay. Task.delay of is an improved version of delay. It’s more in-sync with Roblox’s task scheduler and doesn’t throttle, therefore being more accurate.

You can read more about it here.

Thanks, I looked at it, however don’t know how to even beginning to code that.

Just replace delay with task.delay.

local isDoorOpened = false
local DoorTouched = false
local DoorConnection = nil
while wait(5) do
	--open door
	isDoorOpened = true

	task.delay(10,function()
		isDoorOpened = false
		DoorConnection:Disconnect()
		if not isDoorTouched then
			warn("not touched")
		end
	end)

	DoorConnection = Door["Touched"]:Connect(function()
		DoorTouched = true
	end)

	repeat wait() until isDoorOpened = false
	-- close door
end

like this? but the thing is, the door/part doesn’t disappear, nothing happens, the code is not correct.

Computers do what you tell them to do…

Nowhere in your code do you add a line of code that makes the door disappear.

yeah of course, but i don’t know how to code that properly that’s why I asked on here

Do you even know anything about scripting?

im pretty sure he does, he prob just doesnt know how to debug something

He doesn’t know how to make a door “disappear”.

hmm, ig your right, but still how does do coding when he has no idea how to make a door dissapear?

Here Is a simple code, that can help you…

local door = path.to.door
local openTime = 10 -- how much time before opening.
local closeTime = 10 -- How much time before closing

while task.wait(openTime) do
     door.Transparency = 1
     door.CanCollide = false

     task.delay(closeTime, function()
         door.Transparency = 0
         door.CanCollide = true
     end)
end
1 Like

The reason why your door is not disappearing is because you have no line of code that allows the door to disappear like @CodedJer said you need to first set the door transparency to 1 and can collide to false that way the player can not collide with the door and the door will be invisible
With that being said here’s some reference code to help you get started.

local door = game.workspace.part
local open_time = 10 -- how long you want the door open for before closing
local close_time = 3600 -- how long you want the door closed for before opening

while task.wait(open_time) do
door.Transparency = 1 -- this makes the door completely invisible to all the player's 
door.CanCollide = false -- this will make your door not collidable to any player or object
task.delay(close_time, function()
door.Transparency = 0
door.CanCollide = true
end)
       end 

Hope this is what you are looking for also you may want to check if the player is a humanoid or else the script could error