Checking if a player is already in a table using table.find

I’ve been trying to make a script that checks if a player is already in a table, and if they are then they won’t be added. I tried to use table.find. Out of all the print functions only the first 2 print (print"Inserted 1 player, print"Hello" are the only ones that work)

   while true do 
	if #plrs == 2 then --If there are already 2 people in the table then move on to the next part of the script
		break
	else--otherwise keep running this code until 2 people are in the table
		script.Parent.Touched:Connect(function(hit)-- When the part is hit
			local player = hit.Parent
				
			if player then
				if #plrs == 0 then-- if no players are in the table yet
					print("Inserted 1 player")
					table.insert(plrs,player)--Insert the player into the table
					game.Workspace:WaitForChild(player.Name).Head.BillboardGui.Enabled = true --Enables the player's billboardgui
	
						for i, player in pairs(plrs) do--Loops through the table
							print("Hello")
							
							if not table.find(plrs, player) then--if player is not already in the table..
								print("player not in table")
								table.insert(plrs,player) --.. insert them into the table
								print("another playe inserted")
								print(#plrs)
								game.Workspace:WaitForChild(player.Name).Head.BillboardGui.Enabled = true--enables the gui
							end
						end
				end
			end
		end)		
	end	
	wait()
end

One thing I notice is that you rapidly creating an event connection every time the loop iterates and you are using table.find inside a pairs loop which is unnecessary.

You should clarify what you’re trying to do in the code and fix those mistakes, it is confusingly hard to read the code.

Don’t you have to use table.find in the loop to check if the player is already in the table? Also what parts about it is confusingly hard to read, I’ll try to fix it.

No, it doesn’t have to be in a loop as it checks without the need of one.

And actually it’s a little readable its just weird indentation that confused me, I was just too tired to fully look at it.

I deleted the table.find line and now it keeps adding the same player to the table.

It’s not that you should delete it but that it doesn’t belong in the loop. If you put the if statement containing table.find in the loop, it will check for that value each time the loop runs a turn which is unnecessary because it only needs to be done once.