Is it possible to connect more than one MouseButton1Click event to one button?
I’m trying to make a system where a sound is played when any gui button is pressed.
I don’t want to have to go through each localscript and find each mousebutton1click event, so I’m trying to see if I can write a more general script that plays the sound every time any button is pressed.
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local mouse = player:GetMouse()
uis.InputBegan:Connect(function(input,GPE)
local buttonList = playergui:GetGuiObjectsAtPosition(mouse.X, mouse.Y)
if #buttonList ~= 0 then
for _,x in buttonList do
if x:IsA("ImageButton") then
-- code that fires after pressing any image button here
break
end
end
end
end)
This code detects every GUI element under cursor in the moment of a mouse click/screen tap. Then it iterates through every one of it, and if it is a imagebutton then it runs the code you want. If you want it to detect textbuttons, just add elseif x:IsA(“TextButton”).