Hello, All Text button and image button in Startergui, how to make click sound effects with local script? But there will be no lag or drop. It will understand that I click the button and it will sound. I just want a sound when I click all the buttons with a local script. Thank You.
if you want to play a sound when a button is clicked, simply do
sound:Play()
Put a sound in the Screen GUI, put a local script in the button and put this in.
function PlaySound()
script.Parent.Parent.ClickSound:Play()
end
script.Parent.MouseButton1Click:Connect(PlaySound)
If you wanna make all the buttons that are in your player’s GUI play a sound when you click each one of them, then this is probably the easiest it can get:
wait(0.01)
local gui = game.Players.LocalPlayer.PlayerGui
for i,v in pairs(gui:GetDescendants()) do
if v:IsA("ImageButton") or v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
script["Sound Name Here"]:Play()
end)
end
end
Put this in a localscript in StarterGui, and put the sound you want to play in the local script. Make sure to change “Sound Name Here” to what the sound’s name is.
If you insert another button later in the game, then the script probably wont work on it, but you can still use child added to detect when a button was inserted.
Thank you. Can you make ChildAdded and ChildRemoved Version.
It shouldn’t be too difficult to do yourself. Besides, the way to learn scripting/coding is through doing it.
References for them:
ChildAdded: Instance | Documentation - Roblox Creator Hub
ChildRemoved: Instance | Documentation - Roblox Creator Hub
I did it, do you think it works well?
wait(0.01)
local gui = game.Players.LocalPlayer.PlayerGui
local function GiveEffect()
for i,v in pairs(gui:GetDescendants()) do
if v:IsA("ImageButton") or v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
script.ClickSound:Play()
end)
end
end
end
GiveEffect()
gui.ChildAdded:connect(function()
wait()
GiveEffect()
end)
gui.ChildRemoved:connect(function()
wait()
GiveEffect()
end)
Yes, code seems to look good to me. Does it work as planned?
Yes, It works properly. But my only fear is it will lag or drop the game ?
it wouldnt lag the game because it is neat no local scripts on each button!
How so? It is just a music button. It will not lag your game or drop it unless it is a virus. Nice face and valkyrie helmet by the way!
Ok and thank you so much works well
No problem. Happy to help! Happy scripting!