CesarESP
CesarESP

Reputation: 129

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

Hi im doing a ROBLOX game but my code shows an error

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)

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

do anyone knows how to fix it plz?

sorry if im not good at english.

Upvotes: 0

Views: 2084

Answers (2)

̣̣̣
̣̣̣

Reputation: 33

Additional info to support @Kylaaa's comment

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.

Example: table.remove(testArray, 2)

Upvotes: 0

Kylaaa
Kylaaa

Reputation: 7188

If you used table.insert(table, element) to add all the elements into the first table, the error you are getting is because table.remove(table, index) expects a number for the index to remove, not the element itself. You'll have to iterate over the list to find the element you're looking for.

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)

Upvotes: 1

Related Questions