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

时间:2019-08-28 01:12:10

标签: roblox

您好,我正在玩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(期望的数字,有字符串)

有人知道如何修复它吗?

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

1 个答案:

答案 0 :(得分:0)

如果您使用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)