VincentYee

Main For The Piano

May 27th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. PianoSoundRange = 75
  2. KeyAesthetics = true -- the keys visually press down - why would you want to disable this?
  3.  
  4. ----------------------------------
  5. ----------------------------------
  6. ----------------------------------
  7. ---------PIANO CONNECTION---------
  8. ----------------------------------
  9. ----------------------------------
  10. ----------------------------------
  11.  
  12. ----------------------------------
  13. ------------VARIABLES-------------
  14. ----------------------------------
  15.  
  16. User = nil
  17.  
  18. Bound = script.Parent.Keys.KeyBound
  19. CameraCFrame = CFrame.new(
  20. (Bound.CFrame * CFrame.new(0, 5, 3)).p, -- +z is towards player
  21. (Bound.CFrame * CFrame.new(0, 1, 1)).p
  22. )
  23.  
  24. Connector = game.Workspace:FindFirstChild("GlobalPianoConnector")
  25. if not Connector or not Connector:IsA("RemoteEvent") then
  26. error("The piano requires a RemoteEvent named GlobalPianoConnector to be in Workspace.")
  27. end
  28.  
  29. ----------------------------------
  30. ------------FUNCTIONS-------------
  31. ----------------------------------
  32.  
  33. function Receive(player, action, a, b)
  34. if player == User and action == "play" then
  35. if KeyAesthetics then
  36. HighlightPianoKey(a)
  37. end
  38. Connector:FireAllClients("play", a, Bound.Position, User, PianoSoundRange)
  39. elseif player == User and action == "abort" then
  40. Deactivate()
  41. if SeatWeld then
  42. SeatWeld:remove()
  43. end
  44. end
  45. end
  46. function Activate(player)
  47. Connector:FireClient(player, "activate", CameraCFrame)
  48. User = player
  49. end
  50. function Deactivate()
  51. Connector:FireClient(User, "deactivate")
  52. User = nil
  53. end
  54.  
  55. ----------------------------------
  56. -----------CONNECTIONS------------
  57. ----------------------------------
  58.  
  59. Connector.OnServerEvent:connect(Receive)
  60.  
  61. ----------------------------------
  62. ----------------------------------
  63. ----------------------------------
  64. ----------SEAT MECHANISM----------
  65. ----------------------------------
  66. ----------------------------------
  67. ----------------------------------
  68.  
  69. ----------------------------------
  70. ------------VARIABLES-------------
  71. ----------------------------------
  72.  
  73. Seat = script.Parent.Bench.Seat
  74. SeatWeld = nil
  75.  
  76.  
  77. ----------------------------------
  78. ------------FUNCTIONS-------------
  79. ----------------------------------
  80.  
  81. function WeldChanged(property)
  82. if property == "Parent" and SeatWeld.Parent == nil then
  83. SeatWeld = nil
  84. Deactivate()
  85. BreakSeatConnections()
  86. end
  87. end
  88. function ChildAdded(child)
  89. if child:IsA("Weld") then
  90. local root = child.Part1
  91. local character = root.Parent
  92. local player = game.Players:GetPlayerFromCharacter(character)
  93. if player then
  94. SeatWeld = child
  95. Activate(player)
  96. MakeSeatConnections()
  97. end
  98. end
  99. end
  100.  
  101. ----------------------------------
  102. -----------CONNECTIONS------------
  103. ----------------------------------
  104.  
  105. SeatWeldConnection = nil
  106.  
  107. function MakeSeatConnections()
  108. SeatWeldConnection = SeatWeld.Changed:connect(WeldChanged)
  109. end
  110. function BreakSeatConnections()
  111. SeatWeldConnection:disconnect()
  112. end
  113.  
  114. Seat.ChildAdded:connect(ChildAdded)
  115.  
  116. ----------------------------------
  117. ----------------------------------
  118. ----------------------------------
  119. ------------AESTHETICS------------
  120. ----------------------------------
  121. ----------------------------------
  122. ----------------------------------
  123.  
  124. ----------------------------------
  125. ------------VARIABLES-------------
  126. ----------------------------------
  127.  
  128. Keys = script.Parent.Keys
  129.  
  130. ----------------------------------
  131. ------------FUNCTIONS-------------
  132. ----------------------------------
  133.  
  134. function IsBlack(note)
  135. if note%12 == 2 or note%12 == 4 or note%12 == 7 or note%12 == 9 or note%12 == 11 then
  136. return true
  137. end
  138. end
  139.  
  140. function HighlightPianoKey(note1)
  141. --print("highlight!")
  142. local octave = math.ceil(note1/12)
  143. local note2 = (note1 - 1)%12 + 1
  144. local key = Keys[octave][note2]
  145. if IsBlack(note1) then
  146. key.Mesh.Offset = Vector3.new(0.02, -0.15, 0)
  147. else
  148. key.Mesh.Offset = Vector3.new(0, -.05, 0)
  149. end
  150. delay(.5, function() RestorePianoKey(note1) end)
  151. end
  152. function RestorePianoKey(note1)
  153. local octave = math.ceil(note1/12)
  154. local note2 = (note1 - 1)%12 + 1
  155. local key = Keys[octave][note2]
  156. if IsBlack(note1) then
  157. key.Mesh.Offset = Vector3.new(0.02, -0.1, 0)
  158. else
  159. key.Mesh.Offset = Vector3.new(0, 0, 0)
  160. end
  161. end
Add Comment
Please, Sign In to add comment