Advertisement
HowToRoblox

DoorHandler

Feb 28th, 2023
2,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. local door = script.Parent:WaitForChild("Door")
  2. if not door.PrimaryPart then
  3.     door.PrimaryPart = door:WaitForChild("HINGE")
  4. end
  5.  
  6.  
  7. --CONFIGURATION--
  8. local keycardName = "KEYCARD"
  9.  
  10. local timeOpen = 8
  11.  
  12. local goalRot = 105
  13.  
  14. local openLength = 2
  15. local closeLength = 1.2
  16. -----------------
  17.  
  18. local pos = door:GetPivot().Position
  19. local initialRot = door.PrimaryPart.Orientation.Y
  20.  
  21. local opened = false
  22. local animationHappening = false
  23.  
  24. local prompt = Instance.new("ProximityPrompt")
  25. prompt.ObjectText = "Keycard Door"
  26. prompt.ActionText = "Open door"
  27. prompt.KeyboardKeyCode = Enum.KeyCode.E
  28. prompt.HoldDuration = 0
  29. prompt.RequiresLineOfSight = false
  30. prompt.MaxActivationDistance = 8
  31. prompt.Parent = door
  32.  
  33. local cfv = Instance.new("CFrameValue")
  34. cfv.Value = door:GetPivot()
  35. cfv.Parent = door
  36.  
  37. cfv:GetPropertyChangedSignal("Value"):Connect(function()
  38.     door:PivotTo(cfv.Value)
  39. end)
  40.  
  41.  
  42. function ease(x)
  43.     local n1 = 7.5625
  44.     local d1 = 2.75
  45.  
  46.     if (x < 1 / d1) then
  47.         return n1 * x * x
  48.     elseif (x < 2 / d1) then
  49.         x -= 1.5/d1
  50.         return n1 * (x) * x + 0.75
  51.     elseif (x < 2.5 / d1) then
  52.         x -= 2.25/d1
  53.         return n1 * (x) * x + 0.9375
  54.     else
  55.         x -= 2.625/d1
  56.         return n1 * (x) * x + 0.984375
  57.     end
  58. end
  59.  
  60.  
  61. function closeDoor()
  62.  
  63.     if opened and not animationHappening then
  64.         opened = false
  65.         animationHappening = true
  66.  
  67.         local currentRot = door.PrimaryPart.Orientation.Y
  68.  
  69.         local closeStarted = tick()
  70.         while true do
  71.             local x = (tick() - closeStarted) / closeLength
  72.             local rotOffset = ease(x) * -goalRot
  73.  
  74.             cfv.Value = CFrame.new(pos) * CFrame.Angles(0, math.rad((initialRot + goalRot) + rotOffset), 0)
  75.  
  76.             if x >= 1 then
  77.                 break
  78.             end
  79.  
  80.             game:GetService("RunService").Heartbeat:Wait()
  81.         end
  82.        
  83.         animationHappening = false
  84.         prompt.Enabled = true
  85.     end
  86. end
  87.  
  88. function openDoor()
  89.    
  90.     if not opened and not animationHappening then
  91.         opened = true
  92.         animationHappening = true
  93.        
  94.         prompt.Enabled = false
  95.  
  96.         local openStarted = tick()
  97.        
  98.         while true do
  99.             local x = (tick() - openStarted) / openLength
  100.             local rotOffset = ease(x) * goalRot
  101.  
  102.             cfv.Value = CFrame.new(pos) * CFrame.Angles(0, math.rad(initialRot + rotOffset), 0)
  103.            
  104.             if x >= 1 then
  105.                 break
  106.             end
  107.            
  108.             game:GetService("RunService").Heartbeat:Wait()
  109.         end
  110.        
  111.         animationHappening = false
  112.     end
  113. end
  114.  
  115.  
  116.  
  117. prompt.Triggered:Connect(function(plr)
  118.    
  119.     local char = plr.Character
  120.     if char and char:FindFirstChild("Humanoid") and char.Humanoid.Health > 0 then
  121.        
  122.         if char:FindFirstChild(keycardName) and char[keycardName]:IsA("Tool") then
  123.        
  124.             task.spawn(openDoor)
  125.             task.wait(timeOpen)
  126.             task.spawn(closeDoor)
  127.         end
  128.     end
  129. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement