Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. local CollectionService = game:GetService("CollectionService")
  2.  
  3. ------------------------------------------------------------------------------
  4. -- In this section of the script, we define a Door class. This could be placed in
  5. -- its own independent ModuleScript.
  6.  
  7. -- For more information on this class pattern, check out
  8. -- the object-oriented programming chapter in Programming in Lua:
  9. -- https://www.lua.org/pil/16.html
  10.  
  11. local Door = {}
  12. Door.__index = Door
  13. Door.TAG_NAME = "Door"
  14. Door.OPEN_TIME = 2
  15.  
  16. function Door.new(door)
  17. -- Create a table which will act as our new door object.
  18. local self = {}
  19. -- Setting the metatable allows the table to access
  20. -- the SetOpen, OnTouch and Cleanup methods even if we did not
  21. -- add all of the functions ourself - this is because the
  22. -- __index metamethod is set in the Door metatable.
  23. setmetatable(self, Door)
  24.  
  25. -- Keep track of some door properties of our own
  26. self.door = door
  27. self.debounce = false
  28.  
  29. -- Initialize a Touched event to call a method of the door
  30. self.touchConn = door.Touched:Connect(function (...)
  31. self:OnTouch(...)
  32. end)
  33.  
  34. -- Initialize the state of the door
  35. self:SetOpen(false)
  36.  
  37. -- Print a message so we know when doors are initialized
  38. print("Initialized door: " .. door:GetFullName())
  39.  
  40. return self
  41. end
  42.  
  43. function Door:SetOpen(isOpen)
  44. if isOpen then
  45. self.door.Transparency = .75
  46. self.door.CanCollide = false
  47. else
  48. self.door.Transparency = 0
  49. self.door.CanCollide = true
  50. end
  51. end
  52.  
  53. function Door:OnTouch(part)
  54. if self.debounce then return end
  55. local human = part.Parent:FindFirstChild("Humanoid")
  56. if not human then return end
  57. self.debounce = true
  58. self:SetOpen(true)
  59. wait(Door.OPEN_TIME)
  60. self:SetOpen(false)
  61. self.debounce = false
  62. end
  63.  
  64. function Door:Cleanup()
  65. self.touchConn:disconnect()
  66. self.touchConn = nil
  67. end
  68.  
  69. ------------------------------------------------------------------------------
  70. -- In this section of the script, we want to listen for objects with the door
  71. -- tag. When we find one, create a new Door and keep track of it. When the door
  72. -- tag is removed, we'll find the Door we created and destroy it.
  73.  
  74. local doors = {}
  75.  
  76. local doorAddedSignal = CollectionService:GetInstanceAddedSignal(Door.TAG_NAME)
  77. local doorRemovedSignal = CollectionService:GetInstanceRemovedSignal(Door.TAG_NAME)
  78.  
  79. local function onDoorAdded(door)
  80. if door:IsA("BasePart") then
  81. -- Create a new Door object and save it
  82. -- The door class will take over from here!
  83. doors[door] = Door.new(door)
  84. end
  85. end
  86.  
  87. local function onDoorRemoved(door)
  88. if doors[door] then
  89. -- Clean up an already-existing door
  90. doors[door]:Cleanup()
  91. doors[door] = nil
  92. end
  93. end
  94.  
  95. -- Listen for existing tags, tag additions and tag removals for the door tag
  96. for _,inst in pairs(CollectionService:GetTagged(Door.TAG_NAME)) do
  97. onDoorAdded(inst)
  98. end
  99. doorAddedSignal:Connect(onDoorAdded)
  100. doorRemovedSignal:Connect(onDoorRemoved)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement