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

How to remove a Specific Player from a table?

I don't know why I cant do

table.remove(PlayerTable,Player)

/code/

local PlayerTable = {}
while true do
	for i,v in pairs(PlayerTable)do
		print(v)
		local Player = game.Players[v.Name]
		local Character = Player.Character.Humanoid
		Character.Touched:Connect(function(hit)
			if hit.Name == "Lava"then
				table.remove(PlayerTable,Player)
				print(Player,"Has Died in Lava")
			end
		end)
	end
	wait(.5)
end

4 comments

0

is there an error?

0

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

0

If you set it up as a dictionary you can directly index the player. table["greatneil80"] = nil, orrrrr you can directly index it as a string.

0

How?

2 answers

✓ Accepted answer

table.remove() requires the Index placement of the element to extract—it's numerical location. You gave it a Player Object instead, this is why you're receiving a "Number Expected" exception. To find the unknown Index location of an element within a table, you can use the table.find() function which fortunately locates the index placement of the element given. Use this in conjunction with table.remove() and your script should work just fine!

table.remove(PlayerTable,table.find(PlayerTable, Player))

1 comment

0

Ty :)

I don't know whether this will work because I haven't tested it.

local PlayerTable = {}
while true do
    for i,v in pairs(PlayerTable)do
        print(v)
        local Player = game.Players[v.Name]
        local Character = Player.Character.Humanoid
        Character.Touched:Connect(function(hit)
            if hit.Name == "Lava"then
                table.remove(PlayerTable,i)
                print(Player,"Has Died in Lava")
            end
        end)
    end
    wait(.5)
end

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