Advertisement
qsenko1

Audio Visualiser Modified

Jun 25th, 2022
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1.  
  2. -- to be placed in StarterPlayer > StarterPlayerScripts
  3.  
  4. local Players = game:GetService("Players")
  5.  
  6. -- wait for local player PlayerGui
  7.  
  8. local LocalPlayer = Players.LocalPlayer
  9. local playerGui = LocalPlayer:WaitForChild("PlayerGui")
  10. local PlayerSongEvent = game:GetService("ReplicatedStorage"):FindFirstChild("PlayerSong")
  11.  
  12. local function SongSpawner()
  13. -- create a ScreenGui
  14. local screenGui = Instance.new("ScreenGui", playerGui)
  15. screenGui.ResetOnSpawn = false
  16. -- create a holder for our bar
  17. local frame = Instance.new("Frame", screenGui)
  18. frame.AnchorPoint = Vector2.new(0.5, 0.5)
  19. frame.Position = UDim2.new(0.5, 0, 0.5, 0)
  20. frame.Size = UDim2.new(0.3, 0, 0.05, 0)
  21.  
  22. -- create a bar
  23. local bar = Instance.new("Frame", frame)
  24. bar.Position = UDim2.new(0, 0, 0, 0)
  25. bar.Size = UDim2.new(1, 0, 1, 0)
  26. bar.BackgroundColor3 = Color3.new(0, 1, 0)
  27.    
  28. -- NameText
  29.    
  30. local SongName = Instance.new("TextLabel", frame)  
  31. SongName.Name = "SongName"
  32. SongName.Size = UDim2.new(1,0,1,0)
  33. SongName.Position = UDim2.new(0,0,-1.2,0)
  34. SongName.TextScaled = true
  35. SongName.TextXAlignment = Enum.TextXAlignment.Left
  36. SongName.BackgroundTransparency = 1
  37. SongName.TextColor3 = Color3.new(0,0,0)
  38. SongName.TextStrokeColor3 = Color3.new(255,255,255)
  39. SongName.TextStrokeTransparency = 0
  40. SongName.TextTransparency = 1
  41. SongName.Font = Enum.Font.SourceSansBold
  42.    
  43. -- create buttons
  44. local NextSongButton = Instance.new("ImageButton", screenGui)
  45. NextSongButton.Position = UDim2.new(0.67, 0, 0.56, 0)
  46. NextSongButton.Size = UDim2.new(0.025,0,0.05,0)
  47. NextSongButton.Name = "NextSongButton"
  48. local NextSongCorner = Instance.new("UICorner", NextSongButton)
  49. NextSongCorner.CornerRadius = UDim.new(1,0)
  50. local NextSongBorder = Instance.new("UIStroke", NextSongButton)
  51. NextSongBorder.Thickness = 2
  52. NextSongButton.Image = "http://www.roblox.com/asset/?id=5073738924"
  53.    
  54. local PreviousSongButton = Instance.new("ImageButton", screenGui)
  55. PreviousSongButton.Position = UDim2.new(0.3, 0, 0.56, 0)
  56. PreviousSongButton.Size = UDim2.new(0.025, 0,0.05,0)
  57. PreviousSongButton.Name = "PreviousSongButton"
  58. local PreviousSongCorner = Instance.new("UICorner", PreviousSongButton)
  59. PreviousSongCorner.CornerRadius = UDim.new(1,0)
  60. local PreviousSongBorder = Instance.new("UIStroke", PreviousSongButton)
  61. PreviousSongBorder.Thickness = 2   
  62. PreviousSongButton.Image = "http://www.roblox.com/asset/?id=5073738924"
  63. PreviousSongButton.ImageRectOffset = Vector2.new(1024,0)
  64. PreviousSongButton.ImageRectSize = Vector2.new(-1024,1024)
  65. -- describes what will happen when the song changes?   
  66. local TweenService = game:GetService("TweenService")   
  67.  
  68.    
  69. local function TextVisuals(object,text,length)
  70.     local Info = TweenInfo.new(1)  
  71.     object.TextTransparency = 1
  72.     local Tween = TweenService:Create(object,Info,{TextTransparency = 0})  
  73.     Tween:Play()
  74.     for i = 1,#text,1 do
  75.         object.Text = string.sub(text,1,i)
  76.         wait(length)
  77.     end
  78.     task.wait(2)   
  79.         local Tween2 = TweenService:Create(object,Info,{TextTransparency = 1})
  80.     Tween2:Play()
  81. end
  82.    
  83. local SongChange = true
  84.  
  85. NextSongButton.MouseButton1Up:Connect(function()
  86. --  print("fired") 
  87.     if SongChange == true then
  88.         SongChange = false
  89.         for i,songs in pairs(LocalPlayer.PlayerGui:WaitForChild("LocalSounds"):GetChildren()) do
  90.             print(songs)
  91.             if songs.IsPlaying == true then
  92.                 songs:Stop()
  93.                 else
  94.                    
  95.             end
  96.         end
  97.         SongChange = true  
  98.     end
  99. end)   
  100.  
  101. -- create a sound
  102.  
  103. --local sound = Instance.new("Sound", screenGui)
  104. --sound.SoundId = "rbxassetid://1838673350"
  105. --sound.Looped = true
  106. --sound:Play()
  107. local Value = 1
  108. local SoundsFolder = LocalPlayer.PlayerGui:WaitForChild("LocalSounds"):GetChildren()
  109. print(SoundsFolder[Value])
  110. local sound = SoundsFolder[Value]:Play()
  111.  
  112. PreviousSongButton.MouseButton1Up:Connect(function()
  113.     if SongChange == true then
  114.         SongChange = false
  115.         for i,songs in pairs(LocalPlayer.PlayerGui:WaitForChild("LocalSounds"):GetChildren()) do
  116.             if songs.IsPlaying == true then
  117.                     if Value > 1 then --and Value ~= 2
  118.                     print("Value is greather than 1")
  119.                     if Value == 2 then
  120.                         Value = #SoundsFolder - 1
  121.                         songs:Stop()   
  122.                     else
  123.                         Value = Value - 2
  124.                         songs:Stop()
  125.                     end
  126.              elseif Value == 0 then            
  127.                     print("Value is smaller than 1")
  128.                     songs:Stop()   
  129.                     Value = #SoundsFolder - 1  
  130.                     --Value = 4    
  131.                 end                                
  132.             end        
  133.         end    
  134.         SongChange = true  
  135.     end
  136. end)   
  137.    
  138.  
  139. --[[for i = 1, #SoundsFolder do
  140.     local child = SoundsFolder[i]
  141.     print(child.Name .. " is child number " .. i)
  142. end--]]
  143.  
  144. --local sound = SoundsFolder[Value]--+ 1
  145. --print(sound.Name)
  146. -- define a max loudness
  147. local maxLoudness = 475
  148. local debounce = true
  149. local SongsNumber = #SoundsFolder
  150. -- animate the amplitude bar
  151. print(#SoundsFolder)
  152. task.wait(1)
  153.    
  154. game:GetService("RunService").RenderStepped:Connect(function()
  155.     if SoundsFolder[Value].IsPlaying == false and debounce then --and not debounce--sound.TimeLength >= sound.TimePosition - 1
  156.         debounce = false
  157.         print("debounce got false")
  158.         print(Value)
  159.         if Value >= SongsNumber then
  160.             Value = 1
  161.         --  sound:Play()
  162.             print(Value)   
  163.             task.wait(.1)
  164.             local sound = SoundsFolder[Value]:Play()
  165.             TextVisuals(SongName,"Playing: " ..SoundsFolder[Value].Name ,0.05) 
  166.             debounce = true
  167.         else
  168.             if Value < SongsNumber and not debounce then    -- and debounce == true
  169.         --debounce = true
  170.     --  if Value == 0 then
  171.     --      Value = 1                  
  172.     --  end
  173.         print(#SoundsFolder)
  174.         print(tonumber(Value == SongsNumber))
  175.         --print(tonumber(#SoundsFolder == Value))
  176.         task.wait(1)   
  177.         Value = Value + 1
  178.         print(Value)
  179.         print(SoundsFolder[Value])
  180.         --sound:Stop()
  181.         local sound = SoundsFolder[Value]:Play()
  182.         TextVisuals(SongName,"Playing: " ..SoundsFolder[Value].Name ,0.05)         
  183.     --  sound:Play()
  184.                 --  return
  185.             --  SoundsFolder[Value]:Play()
  186.                 --debounce = true
  187.             end    
  188.             debounce = true
  189.         end    
  190.         else --sound
  191.         --  print("playing")
  192.     local amplitude = math.clamp(SoundsFolder[Value].PlaybackLoudness / maxLoudness, 0, 1)
  193.     bar.Size = UDim2.new(amplitude, 0, 1, 0)
  194. --  print("TimeLength".. SoundsFolder[Value].TimeLength,"TimePosition".. SoundsFolder[Value].TimePosition) -- time and position check!!
  195.     --print(sound.PlaybackLoudness)
  196.     if SoundsFolder[Value].PlaybackLoudness >= 475 then
  197.         bar.BackgroundColor3 = Color3.new(1, 0, 0)
  198.     else
  199.         bar.BackgroundColor3 = Color3.new(0, 1, 0)
  200.     end    
  201.     end
  202. end)
  203.    
  204. end
  205.  
  206. SongSpawner()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement