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.
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 @JackscarIitt does and putting your sounds in SoundService is a good place to put things that only a client will hear.
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
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
Button.MouseButton1Click:Connect(function(Player)
script.Button:Play()
end)
end
end
end
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:
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
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.