Advertisement
RuizuKun_Dev

Area Music Player

Jul 4th, 2020
2,521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. --| SERVICES:
  2.  
  3. local PlayerS = game:GetService("Players")
  4.     local plr = PlayerS.LocalPlayer
  5.         local Cha;
  6.             local Hum;
  7.                 local RootPart;
  8.                
  9. local TweenS = game:GetService('TweenService')
  10.     local TweenInfo_New = TweenInfo.new
  11.    
  12. --| VARIABLES:
  13.                
  14. local Musics = script.Musics
  15. Musics.Parent = game.SoundService
  16.  
  17. local cache = {}
  18. local currentMusic;
  19.  
  20. --| FUNCTIONS:
  21.  
  22. -- Character Handling:
  23.  
  24. local HumDied_Connection, Humanoid_Died;
  25.  
  26. function Humanoid_Died()
  27.     HumDied_Connection:Disconnect(); HumDied_Connection = nil
  28.     if currentMusic then
  29.         currentMusic()
  30.     end
  31. end
  32.  
  33. local function Character_Added(Character)
  34.     if (not cache[Character]) then
  35.         cache[Character] = tick()
  36.         Cha = Character; Hum = Cha:WaitForChild('Humanoid'); RootPart = Hum.RootPart
  37.         HumDied_Connection = Hum.Died:Connect(Humanoid_Died)
  38.     end
  39. end
  40.  
  41. local function Character_Removing(Character)
  42.     Cha:Destroy(); Hum:Destroy();
  43.     Cha, Hum = nil
  44.     cache[Character] = nil
  45. end
  46.  
  47. local function isPointInPart_Func(Point, Part)
  48.     local relPos = Part.CFrame:PointToObjectSpace(Point)
  49.     return math.abs(relPos.X) < Part.Size.X*.5 and math.abs(relPos.Y) < Part.Size.Y*.5 and math.abs(relPos.Z) < Part.Size.Z*.5
  50. end
  51.  
  52. local function isAlive(humanoid)
  53.     return humanoid and humanoid.Health > 0
  54. end
  55.  
  56. --| SCRIPTS:
  57.  
  58. plr.CharacterAdded:Connect(Character_Added); Character_Added(plr.Character or plr.CharacterAdded:Wait());
  59. plr.CharacterRemoving:Connect(Character_Removing);
  60.  
  61. -- Area Music:
  62.  
  63. for _,v in ipairs(Musics:GetChildren()) do
  64.    
  65.     local IsInPart
  66.    
  67.     local part = v.Part
  68.    
  69.     local touched_Fn, touchEnded_Fn
  70.     local touched_Ev, touchEnded_Ev = v.Part.Touched, v.Part.TouchEnded
  71.     local touched_Connection, touchEnded_Connection
  72.    
  73.     local playTween = TweenS:Create(v, TweenInfo_New(1,0,0), {Volume = 5,})
  74.     local stopTween = TweenS:Create(v, TweenInfo_New(2,0,1), {Playing = false, Volume = 0,})
  75.     local diedTween = TweenS:Create(v, TweenInfo_New(2,0,1), {PlaybackSpeed = 0, Volume = .1,})
  76.    
  77.     local function died_Fn()
  78.         diedTween:Play()
  79.         IsInPart = nil
  80.         touchEnded_Connection:Disconnect()
  81.         touched_Connection = touched_Ev:Connect(touched_Fn)
  82.     end
  83.    
  84.     -- Play
  85.     function touched_Fn(touchedPart)
  86.         if isAlive(Hum) and (not IsInPart) and touchedPart == RootPart then
  87.             IsInPart = true
  88.             touched_Connection:Disconnect()
  89.             touchEnded_Connection = touchEnded_Ev:Connect(touchEnded_Fn)
  90.             v.PlaybackSpeed = 1
  91.             v:Play()
  92.             stopTween:Cancel()
  93.             playTween:Play()
  94.             currentMusic = died_Fn
  95.         end
  96.     end
  97.    
  98.     -- Stop
  99.     function touchEnded_Fn(touchedPart)
  100.         if IsInPart and touchedPart == RootPart then
  101.             if (not isPointInPart_Func(RootPart.Position,part)) then
  102.                 IsInPart = nil
  103.                 touchEnded_Connection:Disconnect()
  104.                 touched_Connection = touched_Ev:Connect(touched_Fn)
  105.                 playTween:Cancel()
  106.                 stopTween:Play()
  107.                 currentMusic = nil
  108.             end
  109.         end
  110.     end
  111.    
  112.     touched_Connection = touched_Ev:Connect(touched_Fn)
  113.    
  114.     part.Parent = workspace
  115. end
  116.  
  117. return nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement