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

How would I remove variables from a table?

I am making a sort of disaster game. How would I remove a certain player from a survivor's table?

Would I just do:

table.remove(survivor, #player.Name)

Or:

table.remove(survivor, player.Name)

Assuming that player is somehow already defined as the LocalPlayer.

Also, if I wanted to remove all the players in a table would it be:

table.remove(survivor, #survivor)

1 answer

How you remove elements from a table depends on how you inserted them. A table is a very general data structure that provides a mapping from any data type to any data type.

For example, you could have an array of players where integers are the keys and players are the values {[1] = player1, [2] = player2, etc..}. Or, you could have a dictionary where player names are the keys and player objects are the values {'Bob' = player1, 'ZetaReticuli' = player2, etc..}.

If you want to remove an object by key:

myTable[key] = nil --remove mapping of 'key' to its value in 'myTable'

If you want to remove an object by value:

function RemoveByValue(someTable, someValue)
	for key, value in pairs(someTable) do --iterate over keys and values
		if value = someValue then --find one that matches
			someTable[key] = nil --remove the key that maps to it
			break
		end
	end
end

Only use table.insert and table.remove when you are dealing with arrays - tables whose key values (indices) are integers (1, 2, 3, 4, etc...).

You can remove all of the objects in a table thus:

for key in pairs(someTable)
	someTable[key] = nil
end

If you would like further clarification please show how you insert objects into your survivor table.

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