【问题标题】:Roblox bad argument #2 to 'remove' (number expected, got string)Roblox 错误参数 #2 到“删除”(预期数字,得到字符串)
【发布时间】:2021-10-09 01:50:40
【问题描述】:

您好,我正在玩 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)

错误:“删除”的参数 #2 错误(预期数字,得到字符串)

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

对不起,如果我不擅长英语。

【问题讨论】:

  • 您好 CesarESP,目前尚不清楚您要做什么。你能解释一下你想用你的代码做什么吗?
  • 我有一个包含游戏中所有玩家用户名的表格,我想在他单击按钮时从表格中删除用户。

标签: roblox


【解决方案1】:

如果您使用table.insert(table, element) 将所有元素添加到第一个表中,您得到的错误是因为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)

【讨论】:

    【解决方案2】:

    支持@Kylaaa 评论的其他信息

    Removing Items

    可以使用 Lua 的 table.remove() 函数从数组中删除项目。这将删除指定位置的项目并将任何后续项目向下移动一个索引位置。

    示例:table.remove(testArray, 2)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2019-11-29
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多