I have a few buttons, and I want to play my sound when you click them.
I obviously know how to play my sound, but, I’ve been wondering, is it better to do “script.Parent.Sound:Play()” or something else? I’ve been sticking with that for a pretty long time now, but I don’t want my code messy.
You can use contentprovider to preload your sound ID to make sure it plays on runtime, which is what I do on my loading screens. On top of that, I simply have it in a folder like @Jackscarlett does and putting your sounds in SoundService is a good place to put things that only a client will hear.
That can work, you can also get the children & check if every Object is a TextButton of some sort
You could also organize & set up specific button names if you want them to stop/play/change volume of the sound, but I’ll keep it simple
local Buttons = script.Parent:GetChildren()
local Sounds = game.SoundService:WaitForChild("Sounds")
for _, Button in pairs(Buttons) do
if Button:IsA("TextButton") then
Personaly i’m using “GetDescendant” to play the majority of my gui button sounds.
Then when you need to play the same sound in a lot of buttons, you can get all the descendant of a ScreenGui and play the sound when any button is clicked, it’s a lot easier and faster.
local Menu = script.Parent.Parent:WaitForChild("Menu" ,5)
if Menu ~= nil then
for _,Button in pairs(Menu:GetDescendants())do
if Button:IsA("TextButton") or Button:IsA("ImageButton") then
The best way to avoid that would be to create a modulescript which sets up basic button behaviours for you and executes a function when called for. While @Crygen54 has proposed to run a loop through this, you can already tell there is alot of issues and limitations to his method.
Setting up a proper and modular UI framework would go alot further, but if you are a novice I’d heavily recommend you simply repeat the simple function than trying to do it in a hacky way.