I am trying to make an automatic sliding door. The detecting and opening works but the doors keep stopping mid-way while closing. I’m unable to fix this. Can someone help me?
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LeftDoor = script.Parent.Parent.LeftDoor
local RightDoor = script.Parent.Parent.RightDoor
local LeftDoorCfv = LeftDoor.cfv
local RightDoorCfv = RightDoor.cfv
local ClosedLeftPos = LeftDoor:GetPivot()
local ClosedRightPos = RightDoor:GetPivot()
local OpenedLeftPos = ClosedLeftPos - Vector3.new(3.3, 0, 0)
local OpenedRightPos = ClosedRightPos + Vector3.new(3.3, 0, 0)
local DoorTween = TweenInfo.new(3.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
LeftDoorCfv.Value = ClosedLeftPos
RightDoorCfv.Value = ClosedRightPos
LeftDoorCfv.Changed:Connect(function() LeftDoor:PivotTo(LeftDoorCfv.Value) end)
RightDoorCfv.Changed:Connect(function() RightDoor:PivotTo(RightDoorCfv.Value) end)
local Opened = false
local closeTask = nil
function tweenDoors(targetLeft, targetRight)
TweenService:Create(LeftDoorCfv, DoorTween, {Value = targetLeft}):Play()
TweenService:Create(RightDoorCfv, DoorTween, {Value = targetRight}):Play()
task.wait(DoorTween.Time)
end
function Close()
tweenDoors(ClosedLeftPos, ClosedRightPos)
Opened = false
end
function Open()
Opened = true
tweenDoors(OpenedLeftPos, OpenedRightPos)
if closeTask then task.cancel(closeTask) end
closeTask = task.delay(2, function()
if Opened then Close() end
end)
end
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
Open()
end)
task.delay(4, Open)
I am using models as the doors, and cfv is CFrameValue
Your door tweens keep stopping halfway or act strangely when trying to reopen or close again the main reason is that your script is blocking itself lol.
In your original code you used
task.wait(DoorTween.Time)
inside your tweenDoors function… The problem is that this line makes the whole script yield for the tween duration… That means while the script is waiting you cannot properly cancel tweens or restart them. This is why the doors sometimes freeze or get stuck halfway…
The better solution is to let the tweens run on their own without blocking… TweenService already handles running them in the background so you do not need to wait manually.
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LeftDoor = script.Parent.Parent.LeftDoor
local RightDoor = script.Parent.Parent.RightDoor
local LeftDoorCfv = LeftDoor.cfv
local RightDoorCfv = RightDoor.cfv
local ClosedLeftPos = LeftDoor:GetPivot()
local ClosedRightPos = RightDoor:GetPivot()
local OpenedLeftPos = ClosedLeftPos - Vector3.new(3.3, 0, 0)
local OpenedRightPos = ClosedRightPos + Vector3.new(3.3, 0, 0)
local DoorTween = TweenInfo.new(3.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
LeftDoorCfv.Value = ClosedLeftPos
RightDoorCfv.Value = ClosedRightPos
LeftDoorCfv.Changed:Connect(function() LeftDoor:PivotTo(LeftDoorCfv.Value) end)
RightDoorCfv.Changed:Connect(function() RightDoor:PivotTo(RightDoorCfv.Value) end)
local Opened = false
local closeTask
function tweenDoors(targetLeft, targetRight)
TweenService:Create(LeftDoorCfv, DoorTween, {Value = targetLeft}):Play()
TweenService:Create(RightDoorCfv, DoorTween, {Value = targetRight}):Play()
end
function Close()
tweenDoors(ClosedLeftPos, ClosedRightPos)
Opened = false
end
function Open()
Opened = true
tweenDoors(OpenedLeftPos, OpenedRightPos)
if closeTask then task.cancel(closeTask) end
closeTask = task.delay(2, function()
if Opened then Close() end
end)
end
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
Open()
end)
task.delay(4, Open)
The only change here is that the script no longer waits for the tween duration… Because of that the tweens can run smoothly while the rest of your code SHOULD keep working like cancelling and restarting…
If you are interested here are some useful links:
This might or might not help i dont really know… Let me know if this helps or if you still notice issues!!
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LeftDoor = script.Parent.Parent.LeftDoor
local RightDoor = script.Parent.Parent.RightDoor
local ClosedLeftCFrame = LeftDoor:GetPivot()
local ClosedRightCFrame = RightDoor:GetPivot()
local OpenedLeftCFrame = ClosedLeftCFrame * CFrame.new(-3.3, 0, 0)
local OpenedRightCFrame = ClosedRightCFrame * CFrame.new(3.3, 0, 0)
local DoorTweenInfo = TweenInfo.new(3.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local function tweenModelPivot(model, targetCFrame)
local cfv = Instance.new("CFrameValue")
cfv.Value = model:GetPivot()
local tween = TweenService:Create(cfv, DoorTweenInfo, {Value = targetCFrame})
tween:Play()
cfv:GetPropertyChangedSignal("Value"):Connect(function()
model:PivotTo(cfv.Value)
end)
tween.Completed:Connect(function()
model:PivotTo(targetCFrame)
cfv:Destroy()
end)
end
local Opened = false
local closeTask
local function Close()
tweenModelPivot(LeftDoor, ClosedLeftCFrame)
tweenModelPivot(RightDoor, ClosedRightCFrame)
Opened = false
end
local function Open()
Opened = true
tweenModelPivot(LeftDoor, OpenedLeftCFrame)
tweenModelPivot(RightDoor, OpenedRightCFrame)
if closeTask then task.cancel(closeTask) end
closeTask = task.delay(2, function()
if Opened then Close() end
end)
end
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
Open()
end)
task.delay(4, Open)
I dont know anymore this might fix it… Each tween gets its own CFrameValue now…
Alright this also wouldnt work i made a quick model myself and now it works perfectly!
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LeftDoor = script.Parent.Parent.LeftDoor
local RightDoor = script.Parent.Parent.RightDoor
local ClosedLeft = LeftDoor:GetPivot()
local ClosedRight = RightDoor:GetPivot()
local OpenedLeft = ClosedLeft * CFrame.new(3.3, 0, 0) -- moved right
local OpenedRight = ClosedRight * CFrame.new(-3.3, 0, 0) -- moved left
local TweenInfoSettings = TweenInfo.new(3.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local playersInTrigger = 0
-- Tween for models
local function tweenModel(model, targetCFrame)
local cfValue = Instance.new("CFrameValue")
cfValue.Value = model:GetPivot()
cfValue:GetPropertyChangedSignal("Value"):Connect(function()
model:PivotTo(cfValue.Value)
end)
local tween = TweenService:Create(cfValue, TweenInfoSettings, {Value = targetCFrame})
tween:Play()
tween.Completed:Connect(function()
model:PivotTo(targetCFrame)
cfValue:Destroy()
end)
end
local function OpenDoors()
tweenModel(LeftDoor, OpenedLeft)
tweenModel(RightDoor, OpenedRight)
end
local function CloseDoors()
tweenModel(LeftDoor, ClosedLeft)
tweenModel(RightDoor, ClosedRight)
end
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
playersInTrigger += 1
OpenDoors()
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
playersInTrigger -= 1
if playersInTrigger <= 0 then
playersInTrigger = 0
CloseDoors()
end
end
end)
Models are moved via PivotTo()
If the Door is opening backwards or the wrong way you can always change the OpendLeft and -Right
exmaple: