Unable to tween double doors

Hello everyone. I’m trying to create a script to tween double doors closed by shrinking them, but I’m having some trouble. Only one side of the door opens (the right side). I’ve been stuck on it for a day and was wondering if anyone else can see what the problem is? The broken code is posted below.

-- Services --
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

-- Instances --
local ProximityPrompts = game.Workspace.DoubleDoor.Trigger.ProximityPrompt
local Settings = script.Settings

local rightDoor = game.Workspace.DoubleDoor.RightDoor

-- Variables --
local proximityPrompts = {}
local connections = {}
local rightTween
local leftTween
local isRightOpen = false
local isLeftOpen = false

local shrinkSpeed = Settings.ShrinkSpeed.Value
local rightOpenGoal
local rightClosedGoal

-- Functions --
local function rightSetUp()
	rightClosedGoal = {Size = rightDoor.Size, Position = rightDoor.Position}
	
	local smallSize = Vector3.new((rightDoor.Size.X), (0.05), (rightDoor.Size.Z))
	--local smallPosition = Vector3.new((Block.Position.X), (Block.Position.Y - Block.CFrame.UpVector * Block.Size.Y * -0.5), (Block.Position.Z))
	local smallPosition = rightDoor.Position + rightDoor.CFrame.UpVector * rightDoor.Size.Y * 0.5
	rightOpenGoal = {Size = smallSize, Position = smallPosition}
end

local function getProximityPrompts()
	for index, child in pairs(ProximityPrompts:GetChildren()) do
		if child:IsA("ObjectValue") then
			local prompt = child.Value
			if prompt:IsA("ProximityPrompt") then
				table.insert(proximityPrompts, prompt)
			end
		end
	end
end

local function rightTweenTo(goal)
	if rightTween then
		rightTween:Destroy()
	end
	
	local duration = math.abs((rightDoor.Size.Y - goal.Size.Y) / shrinkSpeed)
	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
	rightTween = TweenService:Create(rightDoor, tweenInfo, goal)
	rightTween:Play()
	if isRightOpen then
		isRightOpen = false
	else
		isRightOpen = true
	end
end

local curretRightAutoCloseConnection
local function cancelRightAutoClose()
	if curretRightAutoCloseConnection then
		curretRightAutoCloseConnection:Disconnect()
	end
end

local function startRightAutoClose(duration)
	cancelRightAutoClose()
	local getTime = os.time
	local startTime = getTime()
	curretRightAutoCloseConnection = RunService.Heartbeat:Connect(function()
		local currentTime = getTime()
		if currentTime > startTime + duration then
			cancelRightAutoClose()
			rightTweenTo(rightClosedGoal)
		end
	end)
end

local COOLDOWN = 2 --Cooldown time
local lastRightTriggeredTime= tick() - COOLDOWN  --You can also set this to zero or any number less than the current time - cooldown
local function onTriggered(player)
	local currentTime = tick() --Get the current time
	if currentTime-lastRightTriggeredTime  > COOLDOWN then --Check if the delta time is greater than the cooldown
		lastRightTriggeredTime = currentTime --Update the lastTriggerTime 
	else
		return --End the function
	end

	if isRightOpen then
		cancelRightAutoClose()
		rightTweenTo(rightClosedGoal)
	else
		rightTweenTo(rightOpenGoal)
		startRightAutoClose(3) --the time the auto close takes
	end
end


-- Code --
rightSetUp()




-- Left Door --

local leftDoor = game.Workspace.DoubleDoor.LeftDoor

local leftOpenGoal
local leftClosedGoal


--Adding sound effects to the door

local sound1 = leftDoor["Sound 1"]
local sound2 = leftDoor["Sound 2"]

--New onTriggered event

local function leftSetUp()
	leftClosedGoal = {Size = leftDoor.Size, Position = leftDoor.Position}

	local smallSize = Vector3.new((leftDoor.Size.X), (0.05), (leftDoor.Size.Z))
	--local smallPosition = Vector3.new((Block.Position.X), (Block.Position.Y - Block.CFrame.UpVector * Block.Size.Y * -0.5), (Block.Position.Z))
	local smallPosition = leftDoor.Position + leftDoor.CFrame.UpVector * leftDoor.Size.Y * 0.5
	leftOpenGoal = {Size = smallSize, Position = smallPosition}
end


local function leftTweenTo(goal)
	if leftTween then
		leftTween:Destroy()
	end

	local duration = math.abs((leftDoor.Size.Y - goal.Size.Y) / shrinkSpeed)
	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
	leftTween = TweenService:Create(leftDoor, tweenInfo, goal)
	leftTween:Play()
	if isLeftOpen then
		isLeftOpen = false
	else
		isLeftOpen = true
	end
end


local curretLeftAutoCloseConnection
local function cancelLeftAutoClose()
	if curretLeftAutoCloseConnection then
		curretLeftAutoCloseConnection:Disconnect()
	end
end

local function startLeftAutoClose(duration)
	cancelLeftAutoClose()
	local getTime = os.time
	local startTime = getTime()
	curretLeftAutoCloseConnection = RunService.Heartbeat:Connect(function()
		local currentTime = getTime()
		if currentTime > startTime + duration then
			cancelLeftAutoClose()
			sound2:Play()
			leftTweenTo(leftClosedGoal)
		end
	end)
end

local COOLDOWN = 2 --Cooldown time
local lastTriggeredTime = tick() - COOLDOWN  --You can also set this to zero or any number less than the current time - cooldown
local function leftOnTriggered(player)
	local currentTime = tick() --Get the current time
	if currentTime-lastTriggeredTime  > COOLDOWN then --Check if the delta time is greater than the cooldown
		lastTriggeredTime = currentTime --Update the lastTriggerTime 
	else
		return --End the function
	end

	if sound1.IsPlaying or sound2.IsPlaying then
		sound1:Stop()
		sound2:Stop()
	end

	if isLeftOpen then
		cancelLeftAutoClose()
		leftTweenTo(leftClosedGoal)
		sound2:Play()
	else
		leftTweenTo(leftOpenGoal)
		startLeftAutoClose(3) --the time the auto close takes
		sound1:Play()
	end
end


-- Code --
leftSetUp()

getProximityPrompts()
for index, prompt in ipairs(proximityPrompts) do
	local connection = prompt.Triggered:Connect(onTriggered)
	table.insert(connections, connection)
end



Hey Salty, I remember you from the obby with Dev_ZG. I read through the script and there is no errors according to me. It all seems alright.
Edit: It was a lot of reading!

Oh hi Flash. Yeah it’s long and it gives no errors either yet it still doesn’t work! I’ll keep trying it though.

Hmm just checked the script but there are no errors though. It should be working perfectly :thinking:

1 Like

Your issue of only the right door opening looks to be an issue with how you connect the function to the Triggered event. You pass in the function for opening the right door, meaning the ProximityPrompts only know that it has to open the right door, nothing happens to leftOnTriggered.

I think you what you need to do is instead of having separate functions for left and right doors, you combine them, specifically for the onTriggered functions because :Connect only accepts 1 function, that way it’ll know to open both doors

2 Likes