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!
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.
– 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
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.
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.
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