Need assistance with door scripting

Hello there,

I am attempting to script a door for a personal project. The door is currently scripted so that it automatically closes after 5 seconds. However, there is also a function in which you can close it early, but when I close and re-open the door using this function, it does not wait the full 5 seconds before closing.

I wish for the door to wait 5 seconds before closing after it has been manually closed and re-opened without it closing any earlier.

If I may please receive some advice on this issue, that would be much appreciated. Thank you! :slightly_smiling_face:

local TweenService = game:GetService("TweenService")

local model = script.Parent
local door = model.Door
local prompt = model.Button.Attachment.ProximityPrompt

local doorCooldown = 5

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad)

local goalOpen = {}
local goalClose = {}
goalOpen.CFrame = door.CFrame * CFrame.new(4.2,0,0)
goalClose.CFrame = door.CFrame
local tweenOpen = TweenService:Create(door, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(door, tweenInfo, goalClose)


prompt.Triggered:Connect(function()
	if prompt.ActionText == "Open" then
		tweenOpen:Play()
		prompt.ActionText = "Close"
		wait(doorCooldown)
		tweenClose:Play()
		prompt.ActionText = "Open"
	elseif prompt.ActionText == "Close" then
		tweenClose:Play()
		prompt.ActionText = "Open"
	end
end)

What I intend for the door to do regardless of automatic or manual (early) close:

What it is actually doing after a manual (early) close:

Could you try delete these lines are see if you still have this issue?

2 Likes

Hey there, thank you for your response.

Yes, the issue is gone, but I wish for the door to close itself after 5 seconds if it isn’t already closed by pressing E. How would I be able to achieve this?

1 Like

Looking into corountines should be useful for this.
If you don’t know how to use these, this tutorial may be helpful.

Hope this helps. :wink:

try adding a closed variable like this:

local doorCooldown = 5

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad)

local goalOpen = {}
local goalClose = {}
goalOpen.CFrame = door.CFrame * CFrame.new(4.2,0,0)
goalClose.CFrame = door.CFrame
local tweenOpen = TweenService:Create(door, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(door, tweenInfo, goalClose)

local closed = true

prompt.Triggered:Connect(function()
	if prompt.ActionText == "Open" then
		closed = false
		tweenOpen:Play()
		prompt.ActionText = "Close"
		wait(doorCooldown)
		if not closed then
			tweenClose:Play()
			prompt.ActionText = "Open"
		end
	elseif prompt.ActionText == "Close" then
		closed = true
		tweenClose:Play()
		prompt.ActionText = "Open"
	end
end)

Hey there, and thank you for your contribution.

Unfortunately, this has not solved my issue. The early closing bug still remains when the door is closed and re-opened before the 5 seconds is up.

@sxmmerniqhts, I have had a look at the video tutorial of coroutines you sent me, and although I understand the concept of it, I’m struggling to implement it into the door script. Would it be okay if you gave me an example? Thank you!

Other than what I last posted, I can only think of one idea for this which in my opinion works well, but could probably be improved.

local doorCooldown = 5

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad)

local goalOpen = {}
local goalClose = {}
goalOpen.CFrame = door.CFrame * CFrame.new(4.2,0,0)
goalClose.CFrame = door.CFrame
local tweenOpen = TweenService:Create(door, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(door, tweenInfo, goalClose)

local opentable = {}
local opentimes = 0

prompt.Triggered:Connect(function()
	if prompt.ActionText == "Open" then
		opentimes += 1
		local opentime = opentimes --to make sure it doesn't get the current number after it is opened again
		opentable[opentime] = true
		tweenOpen:Play()
		prompt.ActionText = "Close"
		wait(doorCooldown)
		if opentable[opentime] then
			tweenClose:Play()
			prompt.ActionText = "Open"
		end
	elseif prompt.ActionText == "Close" then
		opentable[opentimes] = false
		tweenClose:Play()
		prompt.ActionText = "Open"
	end
end)
2 Likes

This code works wonderfully and has resolved my issue. Thank you very much! :smile:

1 Like