Button is cloning more than once

So basically, I’m cloning a button, but it’s cloning more than once for each player; it should clone once for every player. Each player will have each button. Just like Trading you it has one button for each player to send a request.

My script:

MainFrame.GamepassFrame.PlayerList:GetPropertyChangedSignal("Visible"):Connect(function()
        if MainFrame.GamepassFrame.PlayerList.Visible == true then
            for _, otherPlayer in pairs(Players:GetChildren()) do
                if Player.Name ~= otherPlayer.Name then
                    local playerFrame = MainFrame.GamepassFrame.PlayerList.ScrollingFrame:FindFirstChild(otherPlayer.Name)

                    if not playerFrame or not playerFrame:IsA("TextButton") then
                        local PlayerTemplateCloneInstance = PlayerTemplateClone:Clone()
                        PlayerTemplateCloneInstance.Name = otherPlayer.Name
                        PlayerTemplateCloneInstance.TextLabel.Text = otherPlayer.Name

                        local OtherPlayerUserId = Players:GetUserIdFromNameAsync(otherPlayer.Name)
                        local PlayerIcon = Players:GetUserThumbnailAsync(OtherPlayerUserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
                        PlayerTemplateCloneInstance.Icon.Image = PlayerIcon
                        PlayerTemplateCloneInstance.Visible = true
                        PlayerTemplateCloneInstance.Parent = MainFrame.GamepassFrame.PlayerList.ScrollingFrame

                        PlayerTemplateCloneInstance.MouseButton1Down:Connect(function()
                            MainFrame.GamepassFrame.Warning.Text = "Gift " .. MainFrame.GamepassFrame.PlayerList.SelectedGamepass.Value .. " Gamepass to " .. MainFrame.GamepassFrame.PlayerList.SelectedPlayer.Value
                            MainFrame.GamepassFrame.PlayerList.SelectedPlayer.Value = PlayerTemplateCloneInstance.Name
                        end)
                    end
                end
            end
        end
    end)

I hope you can help me.

Is playerFrame for sure a TextButton? If it were, say, an ImageButton, then this line would come back as true:

if not playerFrame or not playerFrame:IsA("TextButton") then

You would want to replace “TextButton” with “ImageButton” in that case (or “GuiButton” which would include both of them).

The instance is a text button not an image button

Is there anything else inside the ScrollingFrame that has the same name as otherPlayer.Name? The FindFirstChild would maybe catch that instead of the button, if there were.

Nope it has only UIListLayout and UIpadding

Let’s break this down one by one to find the problem:

MainFrame.GamepassFrame.PlayerList:GetPropertyChangedSignal("Visible"):Connect(function()
        if MainFrame.GamepassFrame.PlayerList.Visible == true then
            for _, otherPlayer in pairs(Players:GetChildren()) do
                if Player.Name ~= otherPlayer.Name then
                    local playerFrame = MainFrame.GamepassFrame.PlayerList.ScrollingFrame:FindFirstChild(otherPlayer.Name)

                    ...

                    if not playerFrame or not playerFrame:IsA("TextButton") then
                        PlayerTemplateCloneInstance.MouseButton1Down:Connect(function()
                            MainFrame.GamepassFrame.Warning.Text = "Gift " .. MainFrame.GamepassFrame.PlayerList.SelectedGamepass.Value .. " Gamepass to " .. MainFrame.GamepassFrame.PlayerList.SelectedPlayer.Value
                            MainFrame.GamepassFrame.PlayerList.SelectedPlayer.Value = PlayerTemplateCloneInstance.Name
                        end)
                    end
                end
            end
        end
end)

I’ve shrunk the code so we don’t have to see nonrelated lines to this problem. From what I see, the line “PlayerTemplateCloneInstance.MouseButton1Down:Connect(function()” is being connected every time GamepassFrame “Visible” property is changed. If you have this being connected every time it fires, the connection will of course fire multiple times depending on the amount of time the “Visible” property is changing. You want to find a way that if the player were to change “Visible” to false, you can disconnect the function. So your code would be something like this:

local thread

thread = mouse.MouseButton1Down:Connect(function()
   thread:Disconnect()
end)
1 Like