Door stays open

Hello, my door stays open when you quickly go in and out of the detector part, any way to fix this? Code is listed below:

local TweenService = game:GetService("TweenService")
local door1 = script.Parent.door1.doorObject
local door2 = script.Parent.door2.doorObject

local lockValue = game.Workspace.CoreSystems.Values.lockdownValue
local blink = game.Workspace.CoreSystems.Values.alarmSync
----------------------------------------------------
local closedpos1 = Vector3.new(door1.Position.X, door1.Position.Y, door1.Position.Z)
local openpos1 = Vector3.new(door1.Position.X, door1.Position.Y, door1.Position.Z - 3)

local closedpos2 = Vector3.new(door2.Position.X, door2.Position.Y, door2.Position.Z)
local openpos2 = Vector3.new(door2.Position.X, door2.Position.Y, door2.Position.Z + 3)

local movetime = 1 -- Movement speed in seconds.



--local opensfx = script.Parent.doorObject.lockdown_open
--local closesfx = script.Parent.doorObject.lockdown_close
local doorstatus = "opened"
----------------------------------------------------
local CloseProperties1 = {
	Position = closedpos1
}

local OpenProperties1 = {
	Position = openpos1
}

local CloseProperties2 = {
	Position = closedpos2
}

local OpenProperties2 = {
	Position = openpos2
}



local TweenInfoo = TweenInfo.new(movetime,Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local TweenOpen1 = TweenService:Create(door1, TweenInfoo, OpenProperties1)
local TweenClose1 = TweenService:Create(door1, TweenInfoo, CloseProperties1)

local TweenOpen2 = TweenService:Create(door2, TweenInfoo, OpenProperties2)
local TweenClose2 = TweenService:Create(door2, TweenInfoo, CloseProperties2)

----------------------------------------------------






door1.Position = closedpos1



local detector = script.Parent.detector

local function onPartTouched(otherPart)
	local partParent = otherPart.Parent
	
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		doorstatus = "moving"
		TweenOpen1:Play()
		TweenOpen2:Play()
		--opensfx:Play()
		wait(1.5)
		doorstatus = "opened"
	end
end
	
local function OnPartTouchEnded(otherPart)
	local partParent = otherPart.Parent
	-- Look for a humanoid in the parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
		doorstatus = "moving"
		TweenClose1:Play()
		TweenClose2:Play()
		--closesfx:Play()
		wait(1.1)
		doorstatus = "closed"
	end
end


detector.Touched:Connect(onPartTouched)
detector.TouchEnded:Connect(OnPartTouchEnded)
1 Like

I would use lerping if I’d want to make that.

local runService = (game:GetService("RunService"));
local door = (script.Parent);

local isTouched = (false);

local doorGoal = (door.CFrame)
local DOOR_SPEED = (10);
function Update(dt)
	if (isTouched) then
        -- // Door needs to be open.
		doorGoal = ... -- // CFrame on Open
	else
        -- // Door needs to be closed.
		doorGoal = ... -- // CFrame on Close
	end;
	
	door.CFrame = door.CFrame:Lerp(doorGoal, dt * DOOR_SPEED)
end;

runService.Heartbeat:Connect(Update)

local lastTouched
function OnTouch(hit)
	if (hit.Parent:FindFirstChildOfClass("Humanoid")) then
		isTouched = true
		lastTouched = hit.Parent:FindFirstChildOfClass("Humanoid")
	end;
end;

door.Touched:Connect(OnTouch)

function UnTouched(hit)
	if (hit.Parent:FindFirstChildOfClass("Humanoid") == lastTouched) then
		isTouched = true
	end;
end;

door.TouchEnded:Connect(UnTouched)

Yeah no… I really don’t understand what that is supposed to do and i am terrible at CFrame, it’ll just break it without me understanding what broke.

I can give you some references about lerping:

Edit: The thing not mentioned here is that I pass (dt * DOOR_SPEED) as alpha, this is a common method to ‘smooth’ out the lerping movement.