Can't Remove the Player From the Table

Hi,
I have a problem with removing and inserting tables.
The problem is that I inserted the player in a table called playersInObbyTable when I try to remove, it doesn’t seem it’s empty. I set a function detects when the table is empty, it works, but it doesn’t detect the table when the players removed from it.

local function teleportToMap(Spwans)
for i, plr in pairs(players:GetPlayers()) do
	if plr.Character then
		if plr.Character:FindFirstChild("HumanoidRootPart") then
			plr.Character.HumanoidRootPart.CFrame = Spwans[math.random(1,#Spwans)].CFrame + Vector3.new(0,2,0)
			table.insert(playersInObbyTable, plr)
		end
    end
end
end

 endPart.Touched:Connect(function(plr)
	if plr.Parent.Humanoid then
		local plyr = players:GetPlayerFromCharacter(plr.Parent)
		plr.Parent.HumanoidRootPart.CFrame = lobbySpwans[math.random(1,#lobbySpwans)].CFrame + Vector3.new(0,3,0)
		table.insert(winnersTable, plyr)
		playersInObbyTable.plyr = nil
		for i,v in pairs(playersInObbyTable) do
			local playerInTable = functions.GetObjectByValue(playersInObbyTable, plyr)
			if v == plr.Parent and playerInTable then
				playersInObbyTable.playerInTable = nil
			end
		end
	end
end)

I tried to table.remove but that also didn’t work.

1 Like

You are trying to remove the player from the playersInObbyTable when they win?

If so, just do:

table.remove(playersInObbyTable,i)

Also, if you want to detect the amount of players in a table, just do:

if  #playersInObbyTable == 0 then
--Code here
2 Likes

Yeah, but it doesn’t work he needs the index. I won’t know where is it in the table and I tried to check every value then remove it but also that doesn’t work.

I know how to check amount of players but that’s not problem.

1 Like

I’m honestly kind of confused on what you’re trying to do here.

If you’re trying to remove a value from a table, you could simply do:

for j, g in pairs(playersInObbyTable) do
	if g == plyr then
		table.remove(playersInObbyTable, j)
	end
end
2 Likes

I thought about it but it didn’t work too. I am super confused why.

Could you print the table’s content when you’ve added and removed the player from the table?
You can just loop through using for i, v in pairs(table) do print(v) end or use this really neat repr module

2 Likes

Table[Player.Name] = nil will work.

for index, value in pairs(game:GetService("Players"):GetPlayers()) do
   Table[v.Name] = nil
end
1 Like