Help with checking how much things are in a table

Hey there,
So I have this script here that inserts a players name into a table and I was wondering how I can check when there is 2 names in the table and then stop it from letting any other names in the table?

Here is my script:

local sign = script.Parent.Parent
local Gplrs = {}

sign.Touched:Connect(function(touching)
	if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
		local plr = game:GetService("Players"):GetPlayerFromCharacter(touching.Parent)
		Gplrs[#Gplrs+1] = plr.Name
		print(Gplrs[1])
	end
end)
local sign = script.Parent.Parent
local Gplrs = {}
local Duplicates = {}

sign.Touched:Connect(function(touching)
	if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
        
		local plr = game:GetService("Players"):GetPlayerFromCharacter(touching.Parent)
        if Duplicates[plr.Name] then return end
		Gplrs[#Gplrs+1] = plr.Name
        Duplicates[plr.Name] = true
		print(Gplrs[1])
	end
end)

I’m not sure about your use case here, but you might want to connect to PlayerRemoving and remove their name from the table.

1 Like

It works, Thank you so much :slight_smile:

I know this is solved, but you should look into table.insert() and table.find() in the future and you won’t need to double your memory usage.

1 Like

Thanks for your suggestion Ill try them out. Do you have any idea how to check how many names are in the table and then if there are 2 stop others from being added to the table.

if not #Gplrs > 2 then --If Gplrs is not smaller than 2 or in other words is 2 or above
   return
else
   table.insert(Gplrs, plr.Name) --Inserts player name in table
end

PS: Mark the post that solved your problem as solution