Help with using :GetPlayers()

Hi! I’m working on a game right now, and I want a match to start when there are two or more people in the game. How do I do this?

I know that I can use this to see how many players are in a game:

(Players:GetPlayers())

But, I don’t know how to tell a script to do something if there’s a certain amount of players.

3 Likes

The :GetPlayers() method returns a table with indexes to all of the players in the game. This means if there are 5 players, the table would be something like so: {player1, player2, player3, player4, player5}.

To get the length of a table in lua, you simply need to add a # before calling it, so #TableName. If we use this in conjunction with the GetPlayers Method, it’ll return 5 as we have 5 players in our game. We can then use an if statement and check if it’s more than 2, i.e. #plrs >= 2 then

1 Like

I’m a bit confused on this part. Could you please rephrase that?

1 Like

The # syntax simply returns the length of an array, or the amount of entries it has in it.

Lets say we have a table as such:

local shoppingList = {"Banana", "Cupcake", "Baking Soda", "Flour", "Carrot"}

if we wanted to get the length of this list, we’d use our # syntax before calling it. This may be confusing, but in practice it looks like this.

print(#shoppingList) -- Prints 5

This can also easily be applied for your situation, because as we know the GetPlayers Method is simply a table with all the players in it.

Your if statement should look similar to this:

local players = game:GetService("Players"):GetPlayers()
if #players >= 2 then
   -- If there are more than 2 players, run code here
else
   -- If there is less than 2 players, run code here
end
1 Like

Wonderful explanation! One last thing, I tested out the script you provided, but for some reason, the else statement isn’t working

Here’s the script:

while true do
	if #players >= 2 then
		print("people came to my birthday party!")
	else
		print("Only I came to my party :(")
	end
	wait(3)
end

(I’m using a while true do loop with a wait timer to give the players time to load in for my test)

From what I can assume, you’re getting the player count from outside of the loop. This means that it only runs once, making that player count never change. To fix it, simply do this:

while true do
    local players = game:GetService("Players"):GetPlayers() -- This line
	if #players >= 2 then
		print("people came to my birthday party!")
	else
		print("Only I came to my party :(")
	end
	wait(3)
end

That fixed the problem! Thanks.
Simply for the purpose of learning, why do you have to put this inside of the loop?:

local players = game:GetService("Players"):GetPlayers()

Doesn’t really matter if you do

local Players = game:GetService('Players')

or his way, it’s just personal preference.
However, what I did would be better as you don’t need to get the service everytime the code runs.

When you run a script, as you know, it only runs once. If I have code that tells me how to win the game step by step, as seen below, it’ll only run a set amount of times or I’ll have to start copy and pasting the work.

print("punch them in the face!")
print("slap them!!!")
print("you can win this!")
print("punch them in the face!")
print("slap them!!!")
print("you can win this!")

If I were to do that several times, the program would have an insanely large line count for no reason at all. Now, let’s do this with a loop.

local running = true -- If I want to turn the while loop off at any point I can.
while running do
   task.wait(1)  -- A better alternative to wait, using it so my script doesn't exhaust,
   print("punch them in the face!")
   print("slap them!!!")
   print("you can win this!")
end

> "punch them in the face!"
> "slap them!!!"
> "you can win this!"

This will indefinitely (meaning no defined stop, can run infinitely) loop until running is false. However, lets move the first print out of the while loop and see what happens.

local running = true -- If I want to turn the while loop off at any point I can.
print("punch them in the face!")
while running do
   task.wait(1)  -- A better alternative to wait, using it so my script doesn't exhaust,
   print("slap them!!!")
   print("you can win this!")
end

> "punch them in the face!"
> "slap them!!!"
> "you can win this!"
> "slap them!!!"
> "you can win this!"

We notice that it only prints the punch them in the face line once. This is the same issue with your program, as you’re only saying what players is once. If I told you now that I have 15 burgers, but then eat 13 and don’t tell you how would you know? We need to keep the code informed.

You’d put this inside the for loop so it updates the player count every iteration, else its only updating the player count the second the script starts and then never again, as the variable wouldnt change itself just because the player count did

I know its like 3 years later but I couldnt get what benified was getting to so yolo

Thanks! Yeah, this post was a long time ago haha
I’m glad to say I’ve come a long way in scripting since then. Honestly crazy to me that this was three years ago. Thanks for responding and helping those who might be struggling with this in the future.