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!
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:
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?
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)