Connect 不是 TextButton 的有效成员

Connect is not a valid member of TextButton

我想要 ScrollingFrame 中的 roblox 玩家列表,此列表中的每个玩家都是一个按钮,当我单击任何玩家时,他的名字会出现在目标框中 脚本:


for i, v in pairs(game.Players:GetChildren()) do ---gets players in list [PlayersList is a ScrollingFrame] when i join game
    thc = i/25
    local Player = Instance.new("TextButton")
    Player.Name = v.Name
    Player.Parent = PlayersList
    Player.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Player.BackgroundTransparency = 0.800
    Player.Position = UDim2.new(-0.00711364765, 0, thc, 0)
    Player.Size = UDim2.new(0, 100, 0, 30)
    Player.Font = Enum.Font.FredokaOne
    Player.Text = v.Name
    Player.TextColor3 = Color3.fromRGB(255, 255, 255)
    Player.TextScaled = true
    Player.TextSize = 14.000
    Player.TextWrapped = true
end
local plr = game.Players.LocalPlayer
game.Players.PlayerAdded:Connect(function(jplr) ---Adds new player in list
    print(jplr, 'joined')

    for a, b in pairs(PlayersList:GetChildren()) do
        b:Destroy()
    end
    for i, v in pairs(game.Players:GetChildren()) do
        thc = i/25
        local Player = Instance.new("TextButton")
        Player.Name = v.Name
        Player.Parent = PlayersList
        Player.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
        Player.BackgroundTransparency = 0.800
        Player.Position = UDim2.new(-0.00711364765, 0, thc, 0)
        Player.Size = UDim2.new(0, 100, 0, 30)
        Player.Font = Enum.Font.FredokaOne
        Player.Text = v.Name
        Player.TextColor3 = Color3.fromRGB(255, 255, 255)
        Player.TextScaled = true
        Player.TextSize = 14.000
        Player.TextWrapped = true
    end
end)
game.Players.PlayerRemoving:Connect(function(lplr) ---Remove's Players which leave the game
    print(lplr, 'left')

    for a, b in pairs(PlayersList:GetChildren()) do
        b:Destroy()
    end
    for i, v in pairs(game.Players:GetChildren()) do
        if v.Name ~= lplr.Name then
            thc = i/25
            local Player = Instance.new("TextButton")
            Player.Name = v.Name
            Player.Parent = PlayersList
            Player.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
            Player.BackgroundTransparency = 0.800
            Player.Position = UDim2.new(-0.00711364765, 0, thc, 0)
            Player.Size = UDim2.new(0, 100, 0, 30)
            Player.Font = Enum.Font.FredokaOne
            Player.Text = v.Name
            Player.TextColor3 = Color3.fromRGB(255, 255, 255)
            Player.TextScaled = true
            Player.TextSize = 14.000
            Player.TextWrapped = true
        end
    end
end)
for g, n in pairs(PlayersList:GetChildren()) do
    local ind = n.Text
    PlayersList[n.Text]:Connect(function()
        TargBox.Text = n.Name
    end)
end

输出:Connect 不是 TextButton 的有效成员“Players.Artemka_KRYT.PlayerGui.PrisonByGhosty.MenuButton.BackGround.Taravatar.TargBox.PlayersList.Artemka_KRYT”

您需要连接到 TextButton, not the button itself. I would recommend the Activated 事件上的一个事件,因为它会在有人单击按钮时触发,而且它也适用于移动设备。

for g, n in pairs(PlayersList:GetChildren()) do
    local ind = n.Text
    local btn = PlayersList[ind]
    btn.Activated:Connect(function()
        TargBox.Text = n.Name
    end)
end