What's a good/efficient way to play a sound when you click buttons?

Hey everyone,

I have a few buttons, and I want to play my sound when you click them.
image

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.

3 Likes

Personally for me:

Locally, I now refer to my local Sounds inside SoundService

Globally,I just put them inside the workspace for everyone to hear

I just organize them inside Folders for easier use to reference what type I’m searching for

1 Like

Hmmm ok, but how could you play those sounds without doing :Play() on each mousebutton1down function? Should I do something like this:

local tableOButtons = {
button = button
}

for i=1, #tableOButtons  do
     tableOButtons[i].MouseButton1Down:Connect(function()
     -- i play some sound here
     end)
end

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.

1 Like

You have to use :Play(), there is no other way.

On every MouseButton1Down function I don’t wanna do Sound:Play(), so my code doesn’t look messy. Example:

-- Pretend there's a mousebutton1down function here
Sound:Play()

-- Now pretend there's another one
Sound:Play()

-- We will have to continue doing this for each button.

Also, that’s not true because the property Playing exists, which makes the sound play too.

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
        Button.MouseButton1Down:Connect(function()
            Sounds.RandomSound:Play()
        end)
    end
end
1 Like

Ohhhh, ok. I’m new to the “for” and “do” stuff, so I was curious on how I could do this. Thanks!

It’s basically just a simple loop that goes through all of the specified objects you want to search for

This can be really useful for checking differences of values inside tables

local Table = {
    true,
    false
}

for _, Object in pairs(Table) do
    if Object == false then
        print("This is a false object! >:(")
    end
end
1 Like

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.

Capture

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
			Button.MouseButton1Click:Connect(function(Player)
				script.Button:Play()
			end)
		end
	end
end
2 Likes

What’s the difference between GetDescendant and GetChildren? (Again, I’m new to these things)

GetChildren only get all the first children of something
GetDescendant get all children of something, and the children and these children ect… so it get eveything who are in something

For example:
Capture

In the “Menu” ScreenGui, GetChildren will only find the first frames “Tabs - Inventory, Quests, Settings, Shop, Stats”
GetDescendant will find all these frame too + everything else who are inside them

2 Likes

Hmmmmm ok, thanks!

sample text

1 Like

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.

Where did you find a “Loop” in my method ?

You are looping through a table.

for _,Button in pairs(Menu:GetDescendants())do