How to make a sound play every time you click on a button

Hey! I am trying to make a mouse make a sound every time it clicks a button. I’m not wanting to put a script in every button in my game, instead just simple it more by checking if what you clicked is a button.

Would the script start with something like this?

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Mouse = player:GetMouse()

Mouse.Button1Down:Connect(function()
	
end)

Please let me know how I can do this, thank you for you’re time!

Sincerely,

papahetfan

It depends on what type of button you’re talking about

Do you mean a physical 3D button which is using a click detector, a gui, or something else?

Add a “ClickDetector” to a button ‘BasePart’ or ‘Model’ then add a new ‘Script’ in ServerScriptService (not a ‘LocalScript’). And put this in :

local button = change this to path of a button
button.ClickDetector.MouseClick:Connect(function()
    local sound = sound's path
    sound:Play()
end)
local __PARTS = {}
for _,v in pairs(workspace:GetChildren()) do -- Might make a folder that consists of every part that you want to be clicked.
   if v:IsA("Part") then
        local __CLICK = Instance.new("ClickDetector")
        __CLICK.Name = "Click" -- Optional
        __CLICK.Parent = v -- You can also just table.insert the ClickDetectors then use them in a separate function. 
         --[[table.insert(__PARTS,v)--]]
        __CLICK.MouseClick:Connect(function(__PLR: Player?)
           print(__PLR.Name)
      end)
   end
end
--[[local __CLICK = Instance.new("ClickDetector")
__CLICK.Name = "Click"
__CLICK.Parent = __PARTS
__CLICK.MouseClick:Connect(function(__PLR: Player?)
    print(__PLR.Name)
end)--]]

If you want the sound to play locally, just use FireClient(__PLR). Or you can just use the snippet i posted as a localscript.

https://create.roblox.com/docs/reference/engine/classes/UserInputService

Localscript inside of SCS or SPS

– We must get the UserInputService before we can use it
local UserInputService = game:GetService(“UserInputService”)

– A sample function providing one usage of InputBegan
local function onInputBegan(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print(“The left mouse button has been pressed!”)
end
end

UserInputService.InputBegan:Connect(onInputBegan)

I’m talking about the GUI button. Like to open a shop frame for an example.

https://create.roblox.com/docs/reference/engine/classes/TextButton

You can use .MouseButton1Click on a gui button to check when a player clicked on the button or .MouseButton1Down to check when a player is holding their mouse on a gui button.

Button.MouseButton1Click:Connect(function()
    print("Clicked")
    Sound:Play()
end)
2 Likes

Do I have to put it in every button in my game, or is it checking to see if what you clicked is a button? If I have to put in every button, I want to just make 1 script for the player and check to see if what the player clicked was a button or not.

I said this when I first started this topic.

Why do you put and underscore in front of all of your variables? This is such a bad practice and terrible for readability.

Hi, I personally do this approach.

local Buttons = .. -- Define yourself
local ClickSound = .. -- Define yourself
for _, Button in pairs(Buttons) do
	local Debounce = false
	Button.Activated:Connect(function()
		if Debounce then return end
		Debounce = true

		local Sound = ClickSound:Clone()
		Sound.PlayOnRemove = true
		Sound.Parent = Gui
		Sound:Destroy()

		task.wait(0.5)
		Debounce = false
	end
end

The reason for that I personally go for the “PlayOnRemove” method is, that you can spamclick multiple buttons this way, without the sound just “resetting”. The PlayOnRemove feature is a great use for this, since the sound automatically plays when we destroy the object.

Since you use GUI you can loop through all descendants in your “GUI” and check if the descendant is a TextButton or ImageButton

1 Like