Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Remove variable from table?

Basically, I have a table and two variables.

game.ServerStorage["Give Items"].Event:connect(function()
players = {}
Assassin = ""
Protector = ""

And I have it so it gets the players.

function GetPlayers()
    for i,v in pairs(game.Players:GetChildren()) do
        table.insert(players,v)
    end
end

So, then, I continue to get the Assassin and the Protector.

function SelectPlayer()
    local num = math.random(1,#players)
    Assassin = players[num].Name
	table.remove(players, Assassin)
    print('Assassin is: ' ..Assassin)
end

And the table.remove won't remove him from the table, it said it expected a number, not a string. Help?

1 answer

The parameter to table.remove is where the thing to remove is, not what it is.

So, we need to make a simple "find" function which will find Assassin and then remove the thing at that position.

function tableFind(tab,val)
	for k,v in pairs(tab) do
		if v == val then
			return k;
		end
	end
end

...


table.remove(players, tableFind(players, Assassin) );

We could also wrap it into a function if you wanted:

function tableRemoveValue(tab,val)
	return table.remove( tab, tableFind(tab, val) );
end

...

tableRemoveValue(players, Assassin);

1 comment

0

It still said it calling for a number.

This discussion is preserved as it was. New answers, comments, and votes are closed.