How do I check how many players are in a server?

So I’m just trying to have a serverscriptservice script check how many players are in a server. If there is 1, the playmode value in replicatedstorage gets set to 1. If there is more then 1, the playmode value in replicatedstorage gets set to 2. This is so I can disable certain gui elements. But I can’t figure out how to do it or what I’m doing wrong. I’ve gone through different older forum posts and videos but none have worked.

local Players = game:GetService('Players')
local PlayerInServer = #Players:GetPlayers()

local playmode = game.ReplicatedStorage.Playmode.Value

if PlayerInServer == 1 then
	playmode = 1
	print("There is 1 Player, this is Singleplayer.")
elseif PlayerInServer > 1 then
	playmode = 2
	print("There is more then 1 Player, this is Multiplayer.")
end
local players = game:GetService("Players")
local rStorage = game:GetService("ReplicatedStorage")

local mode = rStorage.Playmode --don't read value yet, when you assign to it it won't assign that way

local function onPlayerAdded(player: Player): nil
    mode.Value = #players:GetPlayers()
    print(`There are {mode.Value} players in the game currently.`)
end

players.PlayerAdded:Connect(onPlayerAdded)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.