Button.Activated not working

this code is supposed to fire a remote event when the player clicks an Image button but it doesn’t work for some reason I have tried putting it in a function still doesn’t work.

       local a = 0
				for _, Button2 in pairs(UI.SurgeryUI:GetChildren()) do
					a = a + 1
					print(a)
					if Button2:IsA("GuiButton") then
						print("Button")
						Button2.Activated:Connect(function()
							print("Pressed")
							game.ReplicatedStorage.RemoteEvents.ChatRemote:FireServer(Button2.Name)
						end)
					end
				end

print(a) works
print(“Button”) works
but it doesn’t print(“Pressed”) when I press the button.
btw this for loop is inside another for loop

      for _, Button in pairs(UI.ui:GetChildren()) do
			if Button:IsA("GuiButton") then
				Button.Activated:Connect(function()
					-- the code above
				end)
			end
		end
1 Like

Use the MouseButton1Down Event instead:

        for _, Button in pairs(UI.ui:GetChildren()) do
			if Button:IsA("GuiButton") then
				Button.MouseButton1Down:Connect(function()
					-- the code above
				end)
			end
		end
3 Likes

yes but then it won’t work on mobile

The MouseButton1Down event works on mobile and xbox as well.

2 Likes

@cjjdawg I don’t believe that would work since the MouseButton1 is determined as a left mouse click and MouseButton2 for a right-click.

I’d say that what you are looking for is either TouchTap or TouchLongPress, to determine a touch-enabled devices.

1 Like

It didn’t work the same thing happened

No the MouseButton1Down events works on all platforms.

3 Likes

Roblox has a specific function for a computer mouse which is called MouseButton. The touch-enabled devices don’t have a mouse so the only way you can determine a click is by touch. It just doesn’t make any sense to me if that would work either.

1 Like

I’m telling you it works on all platforms, I have used it before multiple times, those events your referring to are for the UserInputService btw.

1 Like

ok please don’t continue this here I need an answer to my question asap.

You must be doing something else incorrectly then because the code I gave you should work perfectly.

2 Likes

It’s the same code as mine but the only difference is that you used mousebutton1down and that won’t change anything

Yes it will, and ensure the active property of the button is true. The Activated event is not reliable and there is not really any use-case for it.

1 Like

Did you even read the post? that’s not the part that doesn’t work I also tested what you said and it still doesn’t work.

There was some misinformation/misleading information in the responses you got, .Activated works on all platforms and so do mouse related events as of now because the engineers have set it up to do that but it won’t stay like that so it’s suggested you use .Activated.

In response of the actual question in this post, are the buttons active (it’s a property)? I’ve had problems just like this plenty of times and that usually ended up being my problem.

3 Likes

You should use user input service, then check if hes on mobile, pc, xbox…

I don’t think that’s the problem. The problem is that it doesn’t detect the click

Try printing out the buttons name to make sure what you’re trying to click is actually a gui button.

Where did you get this information from? Its false.

Maybe try changing this string to Button2:IsA("ImageButton")? I use a similar method for my UI code and this works for me.

Also I recommend changing pairs to ipairs