How to find how many players are in a table? [FIXED]

Hello everyone! I am a making a round based game and I insert the contestants that are ready to play the game into a table called contestants. Then I want to check if there in enough players to play the game. Here’s part of the script. If you can please help it would be greatly appreciated!

Error occurs when I make the local numberofcontestants

Script
Capture
Heres the output
Capture2

You cant use :GetChildren() on a table. And there’s need for this anyways as the contestants table is a list and you get the size just from that.

until #contestants >= contestantstostart

I tried doing #contestant and it error

I will try again tho!
Thanks

repeat wait() until #game.Players:GetPlayers >= contestantstostart

I dont want people in menu to be counted as a player tho so i didnt do that

repeat wait() until #contaestants >= contestantstostart

On Line 22, you accidentally called GetChildren on a table. You can only call GetChildren on an Instance which returns a table, but you already have a table so there’s no need to use GetChildren. Do this instead:

local numberofcontestants = contestants

Sorry doesn’t error it doesn’t start the game

ok I will try that thx (30…Chars)

1 Like

That cause I spelled ‘contestants’ wrong lol

repeat wait() until #contestants >= contestantstostart

Ik but when there is two players the script doesnt work

The script won’t work because contestants is a static table once the first iteration is complete. You can solve this in many ways. Perhaps the most simple one is to nest the for loop inside the repeat loop. This way, your script will handle almost at real time when a player abandons the menu.

Also, I think you should add a function to delete players from the contestants table when they leave the game (using table.remove). Ignore this if you’re sure this doesn’t apply

I will try this thanks for helping!

Thanks for the help everyone I have fixed the problem!

1 Like

Create a variable to make sure the round has started or not, so you can do this:

local isRound = false
local ingamePlayers = {}
while wait(2) do
      if not isRound then
            for i,v in pairs(game.Players:GetPlayers()) do
                  if not v:FindFirstChild("InMenu") then
                        table.insert(ingamePlayers, v)
                  end
            end
            
            if #ingamePlayers >= contestantsToStart then
                  --Code to run when in game players are over the requirements
            end
      end
end

(Didn’t see your last post but well I will let this in case this helps.)

1 Like

Thx and I’m sure that way works but I figured it out so imma just leave it lol, thx

1 Like