Roblox 错误参数 #2 到 'remove'(预期数字,得到字符串)

Roblox bad argument #2 to 'remove' (number expected, got string)

嗨,我正在玩 ROBLOX 游戏,但我的代码显示错误

players = require(workspace.Players1)
button = script.Parent
player = script.Parent.Parent.Parent.Parent.Parent.Name

function onMouseButton1Down()
    button.Parent.Visible = false
    table.remove(players,player)
    workspace.Cage1.door.Players.Value = workspace.Cage1.door.Players.Value - 1
end

button.MouseButton1Down:connect(onMouseButton1Down)

错误:'remove' 的错误参数 #2(预期数字,得到字符串)

请问有人知道怎么解决吗?

对不起,如果我英语不好。

如果您使用 table.insert(table, element) 将所有元素添加到第一个 table,您得到的错误是因为 table.remove(table, index) 期望索引删除的数字,而不是元素本身。 您必须遍历列表才能找到您要查找的元素。

playerList = require(workspace.Players1)
button = script.Parent
playerName = script.Parent.Parent.Parent.Parent.Parent.Name

function onMouseButton1Down()
    -- hide the button
    button.Parent.Visible = false

    -- remove the player from the list of names
    for i, name in ipairs(playerList) do
        if name == playerName then
            table.remove(playerList, i)
            break
        end
    end

    -- decrease the count of players
    workspace.Cage1.door.Players.Value = 
        workspace.Cage1.door.Players.Value - 1
end

button.MouseButton1Down:Connect(onMouseButton1Down)

支持@Kylaaa 评论的其他信息

Removing Items

An item can be removed from an array with Lua’s table.remove() function. This will remove the item at the specified position and move any following items down one index position.

示例:table.remove(testArray, 2)