Why doesn't the button sound effects work?

I made a local script in Startergui and it’s supposed to play each of the sounds for the events that occur. but for some reason script doesnt work:

local SelectSound = script.SelectSound
local HoverSound = script.HoverSound
local StarterGui = game:GetService("StarterGui")

--Buttons SFX
for _, v in pairs(StarterGui:GetDescendants()) do
	if v:IsA("TextButton") or v:IsA("ImageButton") then
		v.MouseEnter:Connect(function()
			HoverSound:Play()
		end)
		
		v.MouseButton1Click:Connect(function()
			SelectSound:Play()
		end)
	end	
end

Try changing it from “game:GetService(“StarterGui”)” to “game.Players.LocalPlayer.PlayerGui”

2 Likes
local SelectSound = script.SelectSound
local HoverSound = script.HoverSound
local ScreenGui = script:FindFirstAncestorOfClass("ScreenGui")

--Buttons SFX
for _, v in pairs(ScreenGui:GetDescendants()) do
	if v:IsA("TextButton") or v:IsA("ImageButton") then
		v.MouseEnter:Connect(function()
			HoverSound:Play()
		end)
		v.MouseButton1Click:Connect(function()
			SelectSound:Play()
		end)
	end	
end

This will work providing the script is placed somewhere inside the “ScreenGui” instance itself, this is likely what you’re trying to achieve as opposed to dynamically modifying the “PlayerGui” of each “Player” instance.

Thats not it but thanks for the try

What do you mean “thats not it”? It works, I’ve tested myself.

a.rbxl (29.2 KB)

It’s better to modify the StarterGui in this way.

You should only modify the PlayerGui of players if you need to make changes to the UI during runtime (while the game is being played).

Ok thanks but the solution is already provided :slight_smile: