Table.remove/insert is not always working

I’m working on a function where you can remove/insert a player to a table:

function module.PlayerLoser(Player)
	local PlayersPlaying = module["PlayersPlaying"]
	local PlayersNotPlaying = module["PlayersNotPlaying"]
	if table.find(PlayersPlaying, Player.Name) then
		table.remove(PlayersPlaying, Player.Name)
		table.insert(PlayersNotPlaying, Player.Name)
	end
end

However, for any reason, sometimes i get the error that the second argument must be a number, and not a string, while sometimes, this error is not showing and everything works, i’ve tried to remove/insert a value in a different way:

PlayersPlaying[Player.Name] = nil (or) true

But i feel that this way is not an accurate way to fix this, and i would like to use table.insert/remove instead of that. Thanks for reading :slight_smile:

What do you mean this is not “accurate”? To clear an entry you need to set it to nil. table.remove and table.insert are meant for removing/inserting to the array part of a table

You need the numerical index of an element to use table.remove().

Should be something like this:

function module.PlayerLoser(Player)
	local PlayersPlaying = module["PlayersPlaying"]
	local PlayersNotPlaying = module["PlayersNotPlaying"]
	if table.find(PlayersPlaying, Player.Name) then
		table.remove(PlayersPlaying, table.find(PlayersPlaying, Player.Name))
		table.insert(PlayersNotPlaying, Player.Name)
	end
end
1 Like

table.remove takes the numbered index of an array and removes the item at that index. To remove the player’s name from the array, you could use a numeric for loop and remove it that way.

local Names = {"TheeDeathCaster", "Noob", "BestFriend"};

for Count = 1, #Names do
    if (Names[Count] == "Noob") then
        table.remove(Names, Count);
    end
end

print(unpack(Names)); -- TheeDeathCaster BestFriend

If you have any questions don’t be afraid to ask. I hope this helped. :slight_smile: