Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I remove a certain element from a table without saying: table.remove(table.5)?

Asked by 6 years ago

Let's suppose we have the following table with the name of random players:

local SomeRandomPlayers = {"Chris","Milkshake","Something","IDunno","Bob","MeNameJerry"}

What do i do if I want to remove Bob from the table without having to say:

table.remove(SomeRandomPlayers,5)

In the table there will be tons of players added and removed so I will never know wich place Bob is.. Any idea how to do that?

Do I have to do some kind of loop

for i,v in pairs (SomeRandomPlayers) do
if v.Name then
--Still don't know wich place "Bob" is..
end
end

Any help is apreciated!

Thanks!

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

I'm not sure if there is another way but I think this will work

function RemoveFromTable(list,variable)
    for i,v in pairs(list)do
        if v == variable then
            table.remove(list,i)
        end
    end
end

RemoveFromTable(SomeRandomPlayers,"Bob")
0
It worked! Ty mate I'm stupid wilsonsilva007 373 — 6y
Ad

Answer this question