Is there a way for a script to check how many players are in a game?
In my game there's a glitch where if there's only 1 player in the game, they can get as many points as they want. I need some way for the game to check if they're at least 2 players. I'd use that to tell the script to only do its regular cycle if there is more than 1 player in the game. I tried using a way to detect the number of players on a team, but the way the script is structured, it always just ended up breaking the script.
Just wondering if there's a way and if you can link me to some resources so I can learn how.
There are multiple ways:
- Use the # operator in conjunction with :GetPlayers().
- Use game.Players.NumPlayers
.
For the # operator in conjunction with :GetPlayers().
local NumPlayers = #game.Players:GetPlayers();
In your case, you would need to check it before the game starts, don't get confused with the server starting and the game starting. The actual gameplay you've scripted is what I'm talking about.
This is what I think you would need to use:
print("Waiting for two or more players to begin the game."); repeat wait() until #game.Players:GetPlayers() > 1; --You could do >=2 as well. print("The game is now starting!");