Use MouseButton1Down on an array of Buttons

Hi,

I have an array of buttons stored as instances. How would I be able to do MouseButton1Down signal for every button in the array.

Example array:

{
    [1] = TextButton
}

I tried this code, but I didn’t expect it to work:

EdgeInstances.MouseButton1Down:Connect(function()
print("click")
end)

Thanks in advance for any help :+1:

1 Like

You could loop trought the array like this:

ExampleArray = {
[1] = TextButton,
[2] = TextButton2
}

for i, TextButtons in pairs(ExampleArray) do
TextButtons.MouseButton1Down:Connect(function()
print("click")
end)
end 

EDIT: i did a quick test and it works! I can press 2 buttons using only one event.

1 Like

Tested this and it doesn’t seem to work.

Code:

for _, button in pairs(EdgeInstances) do
	button.MouseButton1Down:Connect(function()
		print("click")
	end)
end

Array:

{
    [1] = TextButton
}
1 Like

why is your array like this?, its not defined…

{
    [1] = TextButton
}

it should be:

EdgeInstances = {
    [1] = TextButton
}

oh yeah and make sure “TextButton” is referring to the textbutton you need to click and nothing else

1 Like

Ye sorry I’m just pasting in the output as I’m printing the variable EdgeInstances for debugging.

1 Like

Can you make a print like this please?

print(TextButton.ClassName)

This is to make sure you are reffering to the textbutton…
Another thing to note: the script i used to test was in StarterGui.
This could potentially explain the issue

2 Likes

Just noting, you would want to store the ScriptSignal in another array for when you want to disconnect them.

A more lightweight approach however, would be to detect user input of the mouse and then checking if they pressed a button using BasePlayerGui:GetGuiObjectsAtPosition. Personally this is my preferred method, as you don’t have to continually connect and disconnect button events. Instead you just need to create a very simple UI state system with a state tracker. Then you can store the button elements in a dictionary with their function and just call that function.

2 Likes

ClassName prints as TextButton. I will look into the other things you said.

1 Like

make sure you are searching for the PlayerGui, not the StarterGui, otherwise it won’t work

1 Like

The problem was solved with your previous code and moving it to the StarterGui

2 Likes

This approach seems interesting. Will have a look into this

1 Like

I noticed a flaw in this code only now. This only seems to work for one of the buttons in the array. Is the same happening to you? Any ideas how to fix this?

1 Like

bruh
just make sure you made the array the right way and the buttons are defined like this: script.Parent.TextButton or smth like that.

ExampleArray = {
[1] = script.Parent.TextButton, -- <-- notice the comma
[2] = script.Parent.TextButton2
}
1 Like

Yes everything in the array is correct. I’ve switched over to using @BuilderBob25620 's method now which works fine. And as he stated it should be more lightweight. Thanks anyway.