Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Put this script inside of your scrollingframe where you keep all of your buttons
- --This script should also make it easy to add more buttons
- --You can also change the scrollingframe to a different UI element if you want to
- local SoundService = game:GetService("SoundService") --Access soundservice
- local Sound = Instance.new("Sound") --Makes a new sound
- Sound.Parent = SoundService --Puts it in soundservice to make it global
- Sound.Name = "Music" --Names it Music
- Sound.Looped = true --Ensures its looping
- Sound.Playing = false --Ensures it isnt playing
- local Frame = script.Parent.ScrollingFrame --If you decide to relocate all of your buttons to a different frame, put the directory here
- local function ToggleSong(soundId : string) --Registers a new function called ToggleSong with a parameter called soundId
- if Sound.SoundId == soundId then --Checks if Sound.SoundId is the same as the new soundid
- if Sound.IsPlaying then --If so it just toggles the music on/off
- Sound:Stop()
- else
- Sound:Play()
- end
- else --If it isnt then it starts by stopping the music
- Sound:Stop()
- Sound.SoundId = soundId --Editing the soundid
- Sound:Play() --Playing the sound
- end
- end
- for i, button : GuiButton in pairs(Frame:GetChildren()) do --Loops trough all the buttons in your scrollingframe to allow expandability
- if button:IsA("GuiButton") then --Makes sure its a button
- button.MouseButton1Down:Connect(function() --Fires when the button has been pressed
- local buttonSound = button:FindFirstChild(button.Name) --Finds the buttons sound
- if buttonSound and buttonSound:IsA("Sound") then --Ensures that the sound exists and that it is indeed a sound
- ToggleSong(buttonSound.SoundId) --Fires the ToggleSong function we registered earlier, with the soundid of the button sound
- else
- warn("Sound does not exist or is not a sound") --Warns if the sound doesnt exist or isnt a sound
- end
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment