MouseButton1Click firing more than once

No wonder, that’s the reason, it keeps running the the for loop which causes the issue, remove the heartbeat and your code should work.

if i remove the heartbeat, the for i, v in pairs only fires once, which is when the script starts

How many buttons are in this GUI? If there isn’t an absurd amount, I would create an individual connection for each one that will fire individually.

there isn’t much buttons, but i would add more buttons, so i wanted to make it automatic, the amount varies and depends on the player

Are the buttons being added as the game goes on, or are they already in the GUI when the game starts? (If they are added as the game goes on there is an easy fix for this)

they’re added as the game goes on, and i know the fix to this, but as i said, i wanted to make it automatic, as i would add more buttons throughout the updates

if they are added partway through game playthrough, then you could just do something like this:

gui.ChildAdded:Connect(Function(child)  -- whatever your gui or frame is that the buttons are in
    if child:IsA("ImageButton") or child:IsA("TextButton") then
        child.MouseButton1Click:Connect(function()
              -- do things here
        end)
    end
end) 
1 Like

another question though, why use MouseButton1Click? Do the buttons do different things depending on what button you click them with? If not I would use button.Activated to check. With button.Activated, you can check what they clicked it with and its just generally more reliable.

1 Like

Wouldn’t it be better to instead use " GuiButton" like this, for less redundancy?

gui.ChildAdded:Connect(Function(child)  -- whatever your gui or frame is that the buttons are in
    if child:IsA("GuiButton") then
        child.MouseButton1Click:Connect(function()
              -- do things here
        end)
    end
end) 
2 Likes

yeah it would be, I didn’t even know that check existed lol

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