Elevator Door Problems

Hey,

I’m making an elevator but the closing/opening door mechanic is broken. I can’t seem to get it to work. When the elevator moves, it’s entirely welded but the doors need to close first.

Layout:
image

OpenDoor1 and OpenDoor2 are the doors where the OriginalDoor1 and OriginalDoor2 are going to if they’re going to open. If they are going to close, they’ll tween to PositionDoor1 and PositionDoor2

Code:

function WeldModel(index)
	for i = 1, #index:GetChildren() do
		if index:GetChildren()[i] ~= index.PrimaryPart then
			local Weld = Instance.new("WeldConstraint")
			Weld.Part0 = index:GetChildren()[i]
			Weld.Part1 = index.PrimaryPart
			Weld.Parent = index.PrimaryPart
			index:GetChildren()[i].Anchored = false
		end
	end
end

function GetTime(Distance, Speed)
	local Time = Distance / Speed
	return Time
end

function ElevatorDoors(index, state)
	local elevatorInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
	if state == true then
		local tween1 = TweenService:Create(index.OriginalDoor1, elevatorInfo, {Position = index.OpenDoor1.Position})
		local tween2 = TweenService:Create(index.OriginalDoor2, elevatorInfo, {Position = index.OpenDoor2.Position})
		tween1:Play()
		tween2:Play()
		tween1.Completed:Wait()
		tween2.Completed:Wait()
	elseif state == false then
		local tween1 = TweenService:Create(index.OriginalDoor1, elevatorInfo, {Position = index.PositionDoor1.Position})
		local tween2 = TweenService:Create(index.OriginalDoor2, elevatorInfo, {Position = index.PositionDoor2.Position})
		tween1:Play()
		tween2:Play()
		tween1.Completed:Wait()
		tween2.Completed:Wait()
	end
end

function MoveModel(index, destination, number)
	local NewPoint, Speed = 1, 5
	if destination:FindFirstChild(NewPoint) then
		local NextPoint = destination[number]
		local Distance = (index.PrimaryPart.Position - NextPoint.Position).Magnitude
		local Time = GetTime(Distance, Speed)
		local TweenInformation = TweenInfo.new(Time, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		local Tween = TweenService:Create(index.PrimaryPart, TweenInformation, {CFrame = destination[number].CFrame})
		Tween:Play()
		Tween.Completed:Wait()
		ElevatorDoors(index, false)
	end
end
--Elevator
local ElevatorFolder = workspace.Casino.Elevator
local Elevator = ElevatorFolder.Model
local Desinations = ElevatorFolder.Destinations

local Vaults = Elevator.Vaults
local Ground = Elevator.Ground
local Security = Elevator.Security
local Roof = Elevator.Roof

local elevatorDebounce = false

Roof.ClickDetector.MouseClick:Connect(function()
	if elevatorDebounce == false then
		elevatorDebounce = true
		ElevatorDoors(Elevator, true)
		WeldModel(Elevator)
		MoveModel(Elevator, Desinations, 4)
		elevatorDebounce = false
	end
end)

Any help is appreciated as this is driving me crazy. Thanks!!