Guest User

Untitled

a guest
Jun 1st, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. --Put this script inside of your scrollingframe where you keep all of your buttons
  2.  
  3. --This script should also make it easy to add more buttons
  4. --You can also change the scrollingframe to a different UI element if you want to
  5.  
  6. local SoundService = game:GetService("SoundService") --Access soundservice
  7.  
  8. local Sound = Instance.new("Sound") --Makes a new sound
  9. Sound.Parent = SoundService --Puts it in soundservice to make it global
  10. Sound.Name = "Music" --Names it Music
  11. Sound.Looped = true --Ensures its looping
  12. Sound.Playing = false --Ensures it isnt playing
  13.  
  14. local Frame = script.Parent.ScrollingFrame --If you decide to relocate all of your buttons to a different frame, put the directory here
  15.  
  16. local function ToggleSong(soundId : string) --Registers a new function called ToggleSong with a parameter called soundId
  17.    
  18.     if Sound.SoundId == soundId then --Checks if Sound.SoundId is the same as the new soundid
  19.         if Sound.IsPlaying then --If so it just toggles the music on/off
  20.             Sound:Stop()
  21.         else
  22.             Sound:Play()
  23.         end
  24.     else --If it isnt then it starts by stopping the music
  25.         Sound:Stop()
  26.         Sound.SoundId = soundId --Editing the soundid
  27.         Sound:Play() --Playing the sound
  28.     end
  29. end
  30.  
  31. for i, button : GuiButton in pairs(Frame:GetChildren()) do --Loops trough all the buttons in your scrollingframe to allow expandability
  32.     if button:IsA("GuiButton") then --Makes sure its a button
  33.         button.MouseButton1Down:Connect(function() --Fires when the button has been pressed
  34.            
  35.             local buttonSound = button:FindFirstChild(button.Name) --Finds the buttons sound
  36.              
  37.             if buttonSound and buttonSound:IsA("Sound") then --Ensures that the sound exists and that it is indeed a sound
  38.                 ToggleSong(buttonSound.SoundId) --Fires the ToggleSong function we registered earlier, with the soundid of the button sound
  39.             else
  40.                 warn("Sound does not exist or is not a sound") --Warns if the sound doesnt exist or isnt a sound
  41.             end
  42.                
  43.         end)
  44.     end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment