Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I count how many players there are in a game?

Well, I need to know how to make it so the script knows how many players there are in the game, and if there's not enough it'll say "more members required to play." Yeah, this is of use in one of those "mini-games" games. It'll require a specific amount of players, let's just say four for now.

if Game.Players.GetChildren()
--stuck here lol

5 answers

✓ Accepted answer
local mini = 4

while wait(1) do 
	if game.Players.NumPlayers < mini then 
		print("Waiting for ".. mini - game.Players.NumPlayers.. " more player(s) to join."
	end
end
		

1 comment

0

Hey. When I use numplayers or any other type of player-counting function, it always gives me zero when I'm testing. Is this some sort of testing thing or is it a glitch?

If you want to check the number of players just do :

players = #game:GetService("Players"):GetChildren()

There are quite a few ways to count all the Players. Personally, I just use the Property in game.Players NumPlayers. It has the exact number of Players on the server.

For example:

Players = Game.Players.NumPlayers

But I guess another way is:

Players = #Game.Players:GetChildren()
--or
Players = #Game.Players:children()

but that way gets things like String Values and anything else n Players, so it's better to use this:

Players = #Game.Players:GetPlayers()

But I guess if you want to make a more complex way of counting all the players and completely waste your time, you could make functions for when Players Join and Leave and then Add/Minus 1 each time, like this:

NumberOfPlayers = 0

function NewPlayer(New)
	NumberOfPlayers = NumberOfPlayers + 1
end
Game.Players.PlayerAdded:connect(NewPlayer)


function LessPlayer(Old)
	NumberOfPlayers = NumberOfPlayers - 1
end
Game.Players.PlayerRemoving:connect(LessPlayer)

but I prefer to use Game.Players.NumPlayers, because it's a lot easier. Well, I hope this was useful at all.

** - Kratos232**

This is not so hard at all, just do the following:

if #game.Players:GetChildren() >= numberYouNeed then
--code
end
local Ammount = 0
if #game:service'Players':GetPlayers() < Ammount then
--hue
end

This discussion is preserved as it was. New answers, comments, and votes are closed.