Hello, is there a way to make it where if any text Button is clicker a sound plays?
local SoundService = game:GetService("SoundService")
local screenGui = script.Parent
local sound = script.Sound
for _, v in ipairs(screenGui:GetDescendants()) do
if v:IsA("TextButton") or v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
SoundService:PlayLocalSound(sound)
end)
end
end
Put this script (local script) under your screenGui and place a sound under your script
(This is my first reply on this site so I’m sorry for any mistakes)
Organize all the buttons into a single parent and make it so that whenever one of the Parent’s children are clicked it plays the sound. That’s actually close to how you’ll code efficiently for the rest of your developer life.
Something like:
if clicked.Parent == Parent
---- function
(this is not the actual code, it’s just so you get the mindset that you’ll need to solve this)
local screenGui = script.Parent
local soundService = game:GetService("SoundService")
local sound = soundService:WaitForChild("Sound") --change to name of sound instance inside soundservice
for _, v in ipairs(screenGui:GetDescendants()) do
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
soundService:PlayLocalSound(sound)
end)
end
end
For specifically TextButton instances use this, the script is a local script and can be placed anywhere inside the ScreenGui instance, all you need is a sound instance named “Sound” inside the SoundService directory.