Instead of using while, repeat loops, use for i and lerp.
It's simple. Duplicate the door part two times and name the first one StartPart and the second FinishPart. You should have three door parts but all different names. Position the StartPart and the FinishPart.
(don't forget to make the duplicate parts invisible and cancollide false)
local DoorPart = workspace.Door
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart
Now that you set the variables, get both start and finish parts CFrame.
local DoorPart = workspace.Door
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart
local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame
Next, create a function and use the for i loop.
local DoorPart = workspace.Door
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart
local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame
local Tick = .1
function openDoor()
for i = 0, 1, Tick do
DoorPart.CFrame = DoorPart.CFrame:lerp(FinishPos, i)
wait()
end
DoorPart.CFrame = FinishPos
end
function closeDoor()
for i = 0, 1, Tick do
DoorPart.CFrame = DoorPart.CFrame:lerp(StartPos, i)
wait()
end
DoorPart.CFrame = StartPos
end
Add a click detector in the door to fire the functions
local DoorPart = workspace.Door
local StartPart, FinishPart = workspace.StartPart, workspace.FinishPart
local StartPos, FinishPos = StartPart.CFrame, FinishPos.CFrame
local Tick = .1
function openDoor()
for i = 0, 1, Tick do
DoorPart.CFrame = DoorPart.CFrame:lerp(FinishPos, i)
wait()
end
DoorPart.CFrame = FinishPos
end
function closeDoor()
for i = 0, 1, Tick do
DoorPart.CFrame = DoorPart.CFrame:lerp(StartPos, i)
wait()
end
DoorPart.CFrame = StartPos
end
local IsOpen = false
local ClickDetector = Instance.new("ClickDetector", DoorPart)
ClickDetector.MouseClick:connect(function()
if IsOpen == false then
print("opening door")
openDoor()
IsOpen = true
else
print("closing door")
closeDoor()
IsOpen = false
end
end)