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

How to get the Number of Players in the Server?

I don't want to use GetChildren() because then it returns a Table. I tried using NumPlayers but it returns 0 when there is a person in the server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Status = ReplicatedStorage.SStatus
local Players = game.Players
local IndexOfPlayers = game.Players:GetChildren()
local StartRound
local StartGame
local NumberOfPlayers = Players.NumPlayers
local PlayersNeeded = 2
-- functions

function PlayerEntered(plr)
	local GameGui = plr.PlayerGui:WaitForChild("GameGui")
	if NumberOfPlayers < PlayersNeeded then 
		StartGame = false
		GameGui.Messages.Visible = true
		GameGui.Messages.SystemMessage.Text = ""
		GameGui.Messages:TweenPosition(UDim2.new(0,0,0,10),"In","Linear",3)
		wait(3)
		GameGui.Messages.SystemMessage.TextColor3 = Color3.new(255,0,0)
		local PlayersNeededText = "There needs to be ".. PlayersNeeded .." Players to Start"
		for i = 1, #PlayersNeededText do 
			GameGui.Messages.SystemMessage.Text = string.sub(PlayersNeededText, 1, i)
			wait(0.05)
		end
	else if NumberOfPlayers >= PlayersNeeded then
			if StartGame ~= true then 
				GameGui.Messages.Visible = false
				StartGame = true
			end
		end
	end
end

game.Players.PlayerAdded:connect(function(plr)
	repeat
		while wait(1) do
			PlayerEntered(plr)
			print(NumberOfPlayers)
		end
	until NumberOfPlayers >= PlayersNeeded
end)

3 comments

0

To answer your title, #game.Players:GetPlayers()

0

Thanks

0

NumPlayers is deprecated

1 answer

✓ Accepted answer

#game.Players:GetPlayers()

This returns a table with all players in the game, so all you have to do is add a '#' behind it to get length.

2 comments

0

This is the most simple and easiest answer.

0

This is the most simple and easiest answer.

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