GUI Click Detection Problem

My script is supposed to detect a click on any of the gui in a frame. The clicks aren’t being detected though; any help? Thanks!

for i, item in pairs(script.Parent:GetChildren()) do
	if item ~= script then
		item.MouseButton1Click:Connect(function()
			print(1)
			SellFocus(item)
			print(2)
		end)	
	end
end
1 Like

Only Buttons can receive mouse pressed events (buttons being TextButtons and ImageButtons). What you may want to do, if you’re only looking for Buttons for the player to click, then you could just do this:

for i, item in pairs(script.Parent:GetChildren()) do
	if item:IsA("TextButton") or item:IsA("ImageButton") then
		item.MouseButton1Click:Connect(function()
			print(1)
			SellFocus(item)
			print(2)
		end)	
	end
end

I used the :Activated function of the Gui Button to respond to click events. Have you tried that one instead?

Just tried this, it doesn’t fix it. The script isn’t detecting the clicks.

What does the hierarchy look like? Are all the GUI elements you want to be clicked parented to the same parent as the script?

Yes, they have the same parent, which is a frame.

Just tried this, didn’t work. The script for some reason isn’t detecting the clicks.

Are the GUI elements either TextButtons or ImageButtons? Only objects which are Gui Buttons can actually detect MoustButton1Down, MouseButton1Click, and etc.

They are imagebuttons. They are active, too.

I just tested out the code in studio and got it to work fine. Make sure the script you’re using is a LocalScript if it’s not. The ImageButtons need to be directly parented to the script’s parent as well, and not be, for instance, parented to a frame parented to the script’s parent.

They have the same parent. The script is a localscript.

RobloxStudioBeta_rDublS2FHM This is what I have. I used the same code and it worked fine for me, so I’m not understanding what’s going wrong with yours.

They share the same parent, however the ImageButton is at first parented to the parent of the frame, cloned by another script, and put into a frame.

I don’t understand. May I see more of the code you have so I can better understand what you’re dealing with?

Okay, I fixed it. I used a wait that checks every second if there are imagebuttons.

1 Like