Is their a way to check how many players are in a game?

Hello! So Im trying to make a Elimination game, but you litterly cant come back because if you die or fall of the Baseplate, you get kicked lol. Im trying to make a script that will look for how many players their are in a game, if after the game startup figures out that their is 1 player then, it will say the players name and say “Looks Like “…playername…” Has Won The Game!”

Here’s my current script:

-- Defining Variables
local TutorialText = game.StarterGui.ScreenGui.TutorialText
local BaseplateStrinkScript = game.Workspace.Baseplate.Script
local PlayerGui = game:GetService("PlayerGui")

-- First Line Of Code
BaseplateStrinkScript.Disabled = true

-- Game Startup
wait(5)
PlayerGui.TutorialText.Visible = true
wait(4)
PlayerGui.TutorialText.Text = "In this game, Players must use the cars and Knock eachother off the Baseplate!"
wait(7)
PlayerGui.TutorialText.Text = "The Baseplate Will Strink Slowly so make sure you dont fall!"
wait(7)
PlayerGui.TutorialText.Text = "The Last player Standing Wins!"
wait(5)
PlayerGui.TutorialText.Text = "Goodluck, Players!"
wait(5)
BaseplateStrinkScript.Disabled = false

-- Looking For The Winner
1 Like

Get the table of players from game.Players to find the amount of players in the server.

local playersInServer = #game.Players:GetPlayers()

This returns the numeral value of the player count, but you can remove the hash to get a list of the instances in game.Players

2 Likes
#game.Players:GetPlayers()

that should work to get player count

#game.Players:GetPlayers()

This will work since it gets the amount of the children which are players

Is their a way I can always check this?

Also, can I make it so if their is 1 player their is a local function that makes it so the baseplate script gets disabled BasePlateScriptStrink.Disabled = true?

also what’s the # for? When I saw it, I never knew what it was for

Yes so like this:

local function checkPlayers(players)
    if #players == 1 then
        workspace.Baseplate["WhereveScriptIs"].Disabled = true
   else
       return nil
   end
end

So the # makes the thing into a numerical form so like for e.g. if I have 50 parts in a model I can do print(#model:GetChildren()) this would print 50

Is their a way I can check how many players their are like FAE,

if playersInServer = 1 then

That would error you need to do a == equals

1 Like

Hold up afk, my studio just crashed when I tried to playtest it lol.

1 Like
local function TestFunction()
    BasePlateScriptStrink.Disabled = true
end

game.Players.PlayerAdded:Connect(function()
    if #game.Players:GetPlayers() == 1 then
        TestFunction()
    end
end)

game.Players.PlayerRemoving:Connect(function()
    if #game.Players:GetPlayers() == 1 then
        TestFunction()
    end
end)
1 Like

if you want to figure out how many players there are left, I would use the PlayerRemoving function along with #game.Players:GetPlayers()

local players = game.Players

players.PlayerRemoving:Connect(function()
    if #players:GetPlayers() == 1 then
        local winner = #players:GetPlayers()[1]
        winner.PlayerGui.TutorialText.Text = "Looks like "..winner.Name.." has won the game!"
    end
end)
1 Like

Here is a sample script:

local function checkPlayers(players)
    if #players == 1 then
        workspace.Baseplate["WhereverTheScriptIs"].Disabled = true
    else
        return nil
    end
end

checkPlayers(#game.Players:GetPlayers());
1 Like

When I tried to check how many players their are ( if their was 2 - 20 players in the game then that event would run ) but it backfired.

What do you mean by “backfired”

Oh wait I know why hold on
do this

local function checkPlayers(players)
    if players == 1 then
        workspace.Baseplate["WhereverTheScriptIs"].Disabled = true
    else
        return nil
    end
end

checkPlayers(#game.Players:GetPlayers());
1 Like

Im trying to make it so if their is 2 or 20 players in the game then, the game starts

Here is the error

  21:20:30.298  ServerScriptService.Script:13: Expected 'then' when parsing if statement, got ','  -  Studio - Script:13
  21:20:44.430  0.5, 0.5  -  Server
  21:20:44.752  0.5, 0.5  -  Client
game.Players.PlayerAdded:Connect(function()
if #game.Players:GetChildren() >= 2 then
		--// Do stuff
		print("HI")
	end
end)

This should work, and all it does is check how many children are in game.Players everytime someone joins.

Edit: My bad, didn’t see the date on this post and for some reason it was in latest ;-;

Did you type a , anywhere? :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: