Gamemode That Is Only Selected When There Is One Player

Hey everyone, I was wondering how you would go about making a gamemode which is only selected when there is only one player in the server?
I just don’t know where to start and couldn’t find anything on this anywhere. Any help would be greatly appreciated.

Thanks.

-Oct

local i = 0
for _, v in pairs(game.Players:GetPlayers()) do
i += 1
end
if i == 1 then
print("starting 1 plr gamemode") 
end
1 Like

Even better

local amountOfPlayers = #game.Players:GetPlayers()
2 Likes

During the process of choosing a gamemode, you can check if the number of Players in the Players service is equal to 1:

local Players = game:GetService("Players")

local function chooseGamemode()
    local playerCount = #Players:GetPlayers() -- This uses the length operator which will return a number of how many players are in the game

    if playerCount == 1 then
        -- Choose gamemode that's meant for one player

    elseif playerCount >= 2 then
        -- Choose another gamemode
    end
end

Edit: @octavodad I moved the playerCount variable inside of the function so that it’d be updated accordingly whenever the function is activated. Previously, the variable wouldn’t update since its value was only set when the script first runs.

Ensure that you incorporate that change into your system to prevent the variable from being stuck on a single number!

2 Likes

You can do that easier by doing

local players = game.Players:GetPlayers()

if #players == 1 then
    --Stuff
end
2 Likes

Oof, that’s much better, I guess my brain was thinking about loops lol

1 Like