Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get the Number of Players in the Server?

Asked by
KenUSM 53
6 years ago

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)
0
To answer your title, #game.Players:GetPlayers() XAXA 1569 — 6y
0
Thanks KenUSM 53 — 6y
0
NumPlayers is deprecated Thundermaker300 554 — 6y

1 answer

Log in to vote
1
Answered by
luadotorg 194
6 years ago

#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.

0
This is the most simple and easiest answer. plasma_node 343 — 6y
0
This is the most simple and easiest answer. plasma_node 343 — 6y
Ad

Answer this question