Tween doesn't play, output says completed

Hello everyone,
I’m currently making a sliding door for an airport game. It has a control panel, keycard sensors and sensors on the floor to detect people.
The script mainly works, but in one specific case a tween doesn’t play, but is shown as completed.
If I open the door, then close it but I press open while closing. The door reopens first, but when I press close again, nothing happens. But the tween is still shown as completed.
Can someone help me to fix this tween?
I’m a beginner at scripting, so if you have any questions what I meant in the script, go ahead and ask me :slight_smile:

Thanks in advance

-- CREATED BY TOMATO_PLANET

--------------------------------------------------------
--------------------	SETTINGS	--------------------
--------------------------------------------------------
-----	VARIABLE					--	EXPLANATION
local	timer = 3					--	How many seconds should the door stay open?
local	doorspeed = 2				--	How many seconds the door take to open/close?
local	toolname = "Staff ID"		--	Which tool should open the door?
local	staffgroup = 4601537		--	Members of which group should be able to open the door?
local	staffrank = 20				--	Members of which group rank and above should be able to operate the door?


--------------------------------------------------------
------------	SCRIPT | DO NOT TOUCH		------------
--------------------------------------------------------
local doorleft = script.Parent.DoorLeft.Base
local doorright = script.Parent.DoorRight.Base

--Tool sensors
local tsensor1 = script.Parent.Toolsensor1.Sensor
local tlamp1 = script.Parent.Toolsensor1.Lamp
local tsensor2 = script.Parent.Toolsensor2.Sensor
local tlamp2 = script.Parent.Toolsensor2.Lamp

--Touch sensors
local sensor = script.Parent.Sensor
local insidesensor = script.Parent.InsideSensor
local outsidesensor = script.Parent.OutsideSensor

--Control panel
local paneldoor = script.Parent.ControlPanel.Door.Base
local panelbutton = script.Parent.ControlPanel.Door.Button
local panelbutton2 = script.Parent.ControlPanel.Door.Button2
local panelopen = false
local openbutton = script.Parent.ControlPanel.Frame.Open
local closebutton = script.Parent.ControlPanel.Frame.Close

--Debounce
local debounce = true

local open = "closed"


--------------------------------------------------------
--------------	TWEEN POSITIONS DOOR	----------------
--------------------------------------------------------
local tweenservice = game:GetService("TweenService")
local doortweeninfo = TweenInfo.new(doorspeed,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local openedleft = script.Parent.PositionBlocks.DoorLeftOpened.CFrame
local openedright = script.Parent.PositionBlocks.DoorRightOpened.CFrame
local closedleft = script.Parent.PositionBlocks.DoorLeftClosed.CFrame
local closedright = script.Parent.PositionBlocks.DoorRightClosed.CFrame

local openleftdoor = tweenservice:Create(doorleft,doortweeninfo,{CFrame = openedleft})
local openrightdoor = tweenservice:Create(doorright,doortweeninfo,{CFrame = openedright})
local closeleftdoor = tweenservice:Create(doorleft,doortweeninfo,{CFrame = closedleft})
local closerightdoor = tweenservice:Create(doorright,doortweeninfo,{CFrame = closedright})


--------------------------------------------------------
--------------	TWEEN POSITIONS PANEL	----------------
--------------------------------------------------------
local paneltweeninfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local panelopened = script.Parent.ControlPanel.Positions.DoorOpened.CFrame
local panelclosed = script.Parent.ControlPanel.Positions.DoorClosed.CFrame
local openpanel = tweenservice:Create(paneldoor,paneltweeninfo,{CFrame = panelopened})
local closepanel = tweenservice:Create(paneldoor,paneltweeninfo,{CFrame = panelclosed})

local function openclosepanel(player)
	if player:GetRankInGroup(staffgroup) >= staffrank then
		if panelopen == true then
			panelopen = false
			closepanel:Play()
		else
			panelopen = true
			openpanel:Play()
		end
	end
end

panelbutton.ClickDetector.MouseClick:Connect(function(player)
	openclosepanel(player)
end)

panelbutton2.ClickDetector.MouseClick:Connect(function(player)
	openclosepanel(player)
end)

local sensorinside = script.Parent.Sensor.Inside
local regioninside = sensorinside.Size
regioninside = Region3.new(sensorinside.Position-(regioninside/2),sensorinside.Position+(regioninside/2))
local mininside = #workspace:FindPartsInRegion3(regioninside)

local sensoroutside = script.Parent.Sensor.Outside
local regionoutside = sensoroutside.Size
regionoutside = Region3.new(sensoroutside.Position-(regionoutside/2),sensoroutside.Position+(regionoutside/2))
local minoutside = #workspace:FindPartsInRegion3(regionoutside)

local function dooroperation(dir, stayopen, lamp)
	if debounce then
		debounce = false
		if dir == "open" then
			if open ~= "opened" then
				print("opening")
				if lamp ~= nil then
					lamp.BrickColor = BrickColor.new("Bright green")
				end
				if open == "closing" then
					closerightdoor:Cancel()
					closeleftdoor:Cancel()
					print("cancelled close")
				end
				openleftdoor:Play()
				openrightdoor:Play()
				print("opening now")
				openrightdoor.Completed:wait()
				if lamp ~= nil then
					lamp.BrickColor = BrickColor.new("Black")
				end
				open = "opened"
				print("opened")
				debounce = true
				if stayopen == false then
					dooroperation("close", false)
				end
			end
		elseif dir == "close" then
			if open == "opened" then
				wait(timer)
				open = "closing"
				print("open == closing")
				local closecount = 0
				if stayopen == true then
					while true do
						wait(0.5)
						closecount = closecount+0.5
						print(closecount)
						if #workspace:FindPartsInRegion3(regioninside) == mininside and #workspace:FindPartsInRegion3(regionoutside) == minoutside then
							break
						elseif closecount >= 10 then
							break
						end
					end
				end
				closeleftdoor:Play()
				closerightdoor:Play()
				print("closing now")
				debounce = true
				closerightdoor.Completed:Connect(function(playbackState)
					if playbackState == Enum.PlaybackState.Completed then
						open = "closed"
						print("open == "..open)
					end
				end)
			end
		end
	end
end

tsensor1.Touched:Connect(function(tool)
	if tool.Parent.Name == toolname and tool.Parent.Rank.Value >= staffrank then
			dooroperation("open", false, tlamp1)
	end
end)

tsensor2.Touched:Connect(function(tool)
	if tool.Parent.Name == toolname and tool.Parent.Rank.Value >= staffrank then
		dooroperation("open", false, tlamp2)
	end
end)

openbutton.ClickDetector.MouseClick:Connect(function(player)
	if player:GetRankInGroup(staffgroup) >= staffrank then
		dooroperation("open", true)
	end
end)

closebutton.ClickDetector.MouseClick:Connect(function(player)
	if player:GetRankInGroup(staffgroup) >= staffrank then
		dooroperation("close", true)
	end
end)

sensorinside.Touched:Connect(function()
	insidesensor.Lamp.BrickColor = BrickColor.new("Really red")
	while true do
		wait(0.5)
		if #workspace:FindPartsInRegion3(regioninside) == mininside then
			insidesensor.Lamp.BrickColor = BrickColor.new("Bright green")
			break
		end
	end
end)

sensoroutside.Touched:Connect(function()
	outsidesensor.Lamp.BrickColor = BrickColor.new("Really red")
	while true do
		wait(0.5)
		if #workspace:FindPartsInRegion3(regionoutside) == minoutside then
			outsidesensor.Lamp.BrickColor = BrickColor.new("Bright green")
			break
		end
	end
end)

I got help in a discord server, the fix was to add a open = "opened" in front of my print("opening").

1 Like