How can i detect when the player's mouse clicked on a any gui object

I want to play a sound effect when a gui object is clicked and I want to do this with 1 single script in order to prevent lag. I made an very basic script that detects the player’s mouse click event with user input service but it plays the sound without detecting what the object is being clicked and I don’t know how to detect it. Can someone help me with it? :slightly_smiling_face:

local sounds = game:GetService("ReplicatedStorage"):WaitForChild("Audio"):WaitForChild("UI")
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		sounds:WaitForChild("UI_ButtonSelect"):Play()
	end
end)
local gui_object = -- path

--method1
gui_object.Activated:Connect(function()
   --stuff
end)

--method2
gui_object.MouseButton1Down:Connect(function()
   --stuff
end)

Make sure the gui object is a textbutton, imagebutton, or another type of button.

Then you go to your gui object, and do this.

Button.MouseButton1Click:Connect(function()
--code here
end)

As I undertsand it, @lV0rd and @demisestruth this is not what they want.

They want to make a sound, whenever they click a GUI objects if I read it correctly, and not connect each button itself, and just have one script, that controls this. Try something like this:

local sounds = game:GetService("ReplicatedStorage"):WaitForChild("Audio"):WaitForChild("UI")
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
	local mouse = game.Players.LocalPlayer:GetMouse()
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mouse then
		local GUI = game.Players.LocalPlayer.PlayerGui:GetGuiObjectsAtPosition(mouse.X, mouse.Y)
		local HasClickableObject = false
		for i,v in GUI do 
			if v:IsA("GuiButton") then 
				HasClickableObject = true
			end
		end
		if HasClickableObject == true then
			sounds:WaitForChild("UI_ButtonSelect"):Play()
		end
		
	end
end)
1 Like

You can easily do that in one script too, and more efficiently.

why waste resources for this?? just simply use .Activated and save yourself 15+ lines and resources

Because this works for every GUI button. We aren’t taking about 1 button in my example. we are taking about 10s to 20s of buttons, and if this a GUI heavy game, even more if this is the intended purpose. I am looking at long term effects, not short term. That is what I am assuming unless OP has said anything else

Edit: Title also says “How can i detect when the player’s mouse clicked on a any gui object”, not one object.

Also this means to, if you want to include frames, textlabels etc. as well in my code that I had mentioned above, change: if v:IsA("GuiButton") then to if v:IsA("GuiBase") then. This will include all GUI elements, and not just buttons like my code did above^

1 Like

Thank you so much for your help, that’s exactly what i want

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.