Advertisement
HowToRoblox

ElevatorHandler

Dec 5th, 2020
3,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1. local elevator = script.Parent
  2.  
  3. local cabin = elevator:WaitForChild("Cabin"):WaitForChild("Cabin")
  4. local cabinBtns = cabin.Parent:WaitForChild("Buttons")
  5.  
  6. local callBtns = elevator:WaitForChild("CallButtons")
  7.  
  8. local doors = elevator:WaitForChild("Doors")
  9.  
  10.  
  11. local elevatorQueue = {}
  12. local currentFloor = 1
  13.  
  14.  
  15. local elevatorSpeed = 3
  16. local doorSpeed = 0.5
  17.  
  18. local tweenService = game:GetService("TweenService")
  19. local doorTI = TweenInfo.new(doorSpeed, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
  20.  
  21.  
  22. local elevatorMoving = false
  23.  
  24.  
  25. for i, cabinPart in pairs(cabin.Parent:GetDescendants()) do
  26.    
  27.     if cabinPart:IsA("BasePart") and cabinPart ~= cabin then
  28.        
  29.         local weldConstraint = Instance.new("WeldConstraint")
  30.         weldConstraint.Part0 = cabinPart
  31.         weldConstraint.Part1 = cabin
  32.         weldConstraint.Parent = cabinPart
  33.        
  34.         cabinPart.Anchored = false
  35.     end
  36. end
  37.  
  38.  
  39. for i, btn in pairs(cabinBtns:GetChildren()) do
  40.     Instance.new("ClickDetector", btn)
  41. end
  42. for i, btn in pairs(callBtns:GetChildren()) do
  43.     Instance.new("ClickDetector", btn)
  44. end
  45.  
  46.  
  47. for i, btn in pairs(cabinBtns:GetChildren()) do
  48.    
  49.     btn.ClickDetector.MouseClick:Connect(function()
  50.         table.insert(elevatorQueue, btn.Name)
  51.     end)
  52. end
  53. for i, btn in pairs(callBtns:GetChildren()) do
  54.  
  55.     btn.ClickDetector.MouseClick:Connect(function()
  56.         table.insert(elevatorQueue, btn.Name)
  57.     end)
  58. end
  59.  
  60.  
  61. while true do
  62.    
  63.     local instruction
  64.    
  65.     repeat
  66.         wait()
  67.         instruction = elevatorQueue[1]
  68.     until instruction
  69.  
  70.    
  71.     if instruction == "OpenDoorsButton" and not elevatorMoving then
  72.        
  73.         local lDoor = doors["Floor" .. currentFloor].LeftDoor
  74.         tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorOpened.Size, CFrame = lDoor.LeftDoorOpened.CFrame}):Play()
  75.         local rDoor = doors["Floor" .. currentFloor].RightDoor
  76.         tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorOpened.Size, CFrame = rDoor.RightDoorOpened.CFrame}):Play()
  77.        
  78.         wait(doorSpeed)
  79.         table.remove(elevatorQueue, 1)
  80.        
  81.        
  82.     elseif instruction == "CloseDoorsButton" and not elevatorMoving then
  83.        
  84.         local lDoor = doors["Floor" .. currentFloor].LeftDoor
  85.         tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorClosed.Size, CFrame = lDoor.LeftDoorClosed.CFrame}):Play()
  86.         local rDoor = doors["Floor" .. currentFloor].RightDoor
  87.         tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorClosed.Size, CFrame = rDoor.RightDoorClosed.CFrame}):Play()
  88.        
  89.         wait(doorSpeed)
  90.        
  91.         table.remove(elevatorQueue, 1)
  92.        
  93.        
  94.        
  95.     elseif instruction == "CallButton1" or instruction == "Floor1Button" then
  96.        
  97.         elevatorMoving = true
  98.        
  99.        
  100.         if currentFloor ~= 1 then
  101.            
  102.            
  103.             local lDoor = doors["Floor" .. currentFloor].LeftDoor
  104.             tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorClosed.Size, CFrame = lDoor.LeftDoorClosed.CFrame}):Play()
  105.             local rDoor = doors["Floor" .. currentFloor].RightDoor
  106.             tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorClosed.Size, CFrame = rDoor.RightDoorClosed.CFrame}):Play()
  107.  
  108.             wait(doorSpeed)
  109.  
  110.            
  111.             cabin.ElevatorSound:Play()
  112.            
  113.             local elevatorTI = TweenInfo.new(elevatorSpeed * math.abs(currentFloor - 1), Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
  114.             tweenService:Create(cabin, elevatorTI, {CFrame = cabin.CFrame + Vector3.new(0, (doors["Floor1"].RightDoor.RightDoor.Position.Y - cabin.Position.Y), 0)}):Play()
  115.            
  116.            
  117.             wait(elevatorSpeed * math.abs(currentFloor - 1))
  118.            
  119.             cabin.ElevatorSound:Stop()
  120.             cabin.ElevatorPing:Play()
  121.         end
  122.        
  123.        
  124.         local lDoor = doors["Floor1"].LeftDoor
  125.         tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorOpened.Size, CFrame = lDoor.LeftDoorOpened.CFrame}):Play()
  126.         local rDoor = doors["Floor1"].RightDoor
  127.         tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorOpened.Size, CFrame = rDoor.RightDoorOpened.CFrame}):Play()
  128.  
  129.         wait(doorSpeed)
  130.        
  131.        
  132.         currentFloor = 1
  133.         elevatorMoving = false
  134.         table.remove(elevatorQueue, 1)
  135.        
  136.        
  137.     elseif instruction == "CallButton2" or instruction == "Floor2Button" then
  138.  
  139.         elevatorMoving = true
  140.  
  141.  
  142.         if currentFloor ~= 2 then
  143.  
  144.  
  145.             local lDoor = doors["Floor" .. currentFloor].LeftDoor
  146.             tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorClosed.Size, CFrame = lDoor.LeftDoorClosed.CFrame}):Play()
  147.             local rDoor = doors["Floor" .. currentFloor].RightDoor
  148.             tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorClosed.Size, CFrame = rDoor.RightDoorClosed.CFrame}):Play()
  149.  
  150.             wait(doorSpeed)
  151.            
  152.            
  153.             cabin.ElevatorSound:Play()
  154.            
  155.             local elevatorTI = TweenInfo.new(elevatorSpeed * math.abs(currentFloor - 2), Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
  156.             tweenService:Create(cabin, elevatorTI, {CFrame = cabin.CFrame + Vector3.new(0, (doors["Floor2"].RightDoor.RightDoor.Position.Y - cabin.Position.Y), 0)}):Play()
  157.  
  158.            
  159.             wait(elevatorSpeed * math.abs(currentFloor - 2))
  160.            
  161.             cabin.ElevatorSound:Stop()
  162.             cabin.ElevatorPing:Play()          
  163.         end
  164.  
  165.  
  166.         local lDoor = doors["Floor2"].LeftDoor
  167.         tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorOpened.Size, CFrame = lDoor.LeftDoorOpened.CFrame}):Play()
  168.         local rDoor = doors["Floor2"].RightDoor
  169.         tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorOpened.Size, CFrame = rDoor.RightDoorOpened.CFrame}):Play()
  170.  
  171.         wait(doorSpeed)
  172.  
  173.  
  174.         currentFloor = 2
  175.         elevatorMoving = false
  176.         table.remove(elevatorQueue, 1)
  177.        
  178.        
  179.     elseif instruction == "CallButton3" or instruction == "Floor3Button" then
  180.  
  181.         elevatorMoving = true
  182.  
  183.  
  184.         if currentFloor ~= 3 then
  185.            
  186.  
  187.             local lDoor = doors["Floor" .. currentFloor].LeftDoor
  188.             tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorClosed.Size, CFrame = lDoor.LeftDoorClosed.CFrame}):Play()
  189.             local rDoor = doors["Floor" .. currentFloor].RightDoor
  190.             tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorClosed.Size, CFrame = rDoor.RightDoorClosed.CFrame}):Play()
  191.  
  192.             wait(doorSpeed)
  193.            
  194.            
  195.             cabin.ElevatorSound:Play()
  196.  
  197.             local elevatorTI = TweenInfo.new(elevatorSpeed * math.abs(currentFloor - 3), Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
  198.             tweenService:Create(cabin, elevatorTI, {CFrame = cabin.CFrame + Vector3.new(0, (doors["Floor3"].RightDoor.RightDoor.Position.Y - cabin.Position.Y), 0)}):Play()
  199.  
  200.  
  201.             wait(elevatorSpeed * math.abs(currentFloor - 3))
  202.            
  203.             cabin.ElevatorSound:Stop()
  204.             cabin.ElevatorPing:Play()
  205.         end
  206.  
  207.  
  208.         local lDoor = doors["Floor3"].LeftDoor
  209.         tweenService:Create(lDoor.LeftDoor, doorTI, {Size = lDoor.LeftDoorOpened.Size, CFrame = lDoor.LeftDoorOpened.CFrame}):Play()
  210.         local rDoor = doors["Floor3"].RightDoor
  211.         tweenService:Create(rDoor.RightDoor, doorTI, {Size = rDoor.RightDoorOpened.Size, CFrame = rDoor.RightDoorOpened.CFrame}):Play()
  212.  
  213.         wait(doorSpeed)
  214.  
  215.  
  216.         currentFloor = 3
  217.         elevatorMoving = false
  218.         table.remove(elevatorQueue, 1)
  219.     end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement