How to get player count?

This prints 0 players

local Players = game:GetService("Players")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

while true do
	repeat
		print("players: "..playerCount)
		wait(1)
	until playerCount >= 1
	wait(5)
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)
local Players = game:GetService("Players")
playerCount = 0

local function updatePlayerCount(player)
	print("Player entered")
	local players = Players:GetChildren()
	playerCount = #players
end
Players.PlayerAdded:Connect(updatePlayerCount) -- Here
Players.PlayerRemoving:Connect(updatePlayerCount) -- And here

while true do
	repeat
		print("players: "..playerCount)
		wait(1)
	until playerCount >= 1
	wait(5)
end

-- Used to be here

this should work

1 Like
local Players = game:GetService("Players")

local PlayerCount = 0

local function UpdPlrCnt(Player)
	PlayerCount = #Players:GetPlayers()
	print(PlayerCount)
end

Players.PlayerAdded:Connect(UpdPlrCnt)
Players.PlayerRemoving:Connect(UpdPlrCnt)

while true do loops yield the entire thread (prevents further code from being executed).

yea but i needed that loop for waiting for players/intermission between rounds

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

while true do
	if ReplicatedStorage.GameState.Value ~= "InProgress" then
		repeat
			wait()
			ReplicatedStorage.GameState.Value = "Waiting for players"
		until playerCount >= 1
		ReplicatedStorage.GameState.Value = "Intermission"
		print(ReplicatedStorage.GameState.Value)
		wait(10)
		ReplicatedStorage.GameState.Value = "InProgress"
		print(ReplicatedStorage.GameState.Value)
	end
	wait(10)
	ReplicatedStorage.GameState.Value = "Waiting for players"
	print(ReplicatedStorage.GameState.Value)
end

code for now looks like this

Wrap it inside task.spawn(function()) or coroutine.wrap() etc. Those functions create alternate threads to the main thread which can be tasked with the execution of what otherwise would be yielding code.

never used coroutines, how would they go in my code?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

while true do
	if ReplicatedStorage.GameState.Value ~= "InProgress" then
		repeat
			wait()
			ReplicatedStorage.GameState.Value = "Waiting for players"
		until playerCount >= 1
		ReplicatedStorage.GameState.Value = "Intermission"
		print(ReplicatedStorage.GameState.Value)
		wait(10)
		ReplicatedStorage.GameState.Value = "InProgress"
		print(ReplicatedStorage.GameState.Value)
	end
	wait(10)
	ReplicatedStorage.GameState.Value = "Waiting for players"
	print(ReplicatedStorage.GameState.Value)
end

--code here wont run
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

coroutine.resume(coroutine.create(function()
		while true do
		if ReplicatedStorage.GameState.Value ~= "InProgress" then
			repeat
				wait()
				ReplicatedStorage.GameState.Value = "Waiting for players"
			until playerCount >= 1
			ReplicatedStorage.GameState.Value = "Intermission"
			print(ReplicatedStorage.GameState.Value)
			wait(10)
			ReplicatedStorage.GameState.Value = "InProgress"
			print(ReplicatedStorage.GameState.Value)
		end
		wait(10)
		ReplicatedStorage.GameState.Value = "Waiting for players"
		print(ReplicatedStorage.GameState.Value)
	end
end)

--code here will run
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

coroutine.wrap(function()
		while true do
		if ReplicatedStorage.GameState.Value ~= "InProgress" then
			repeat
				wait()
				ReplicatedStorage.GameState.Value = "Waiting for players"
			until playerCount >= 1
			ReplicatedStorage.GameState.Value = "Intermission"
			print(ReplicatedStorage.GameState.Value)
			wait(10)
			ReplicatedStorage.GameState.Value = "InProgress"
			print(ReplicatedStorage.GameState.Value)
		end
		wait(10)
		ReplicatedStorage.GameState.Value = "Waiting for players"
		print(ReplicatedStorage.GameState.Value)
	end
end)()

--code here will run
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

task.spawn(function()
		while true do
		if ReplicatedStorage.GameState.Value ~= "InProgress" then
			repeat
				wait()
				ReplicatedStorage.GameState.Value = "Waiting for players"
			until playerCount >= 1
			ReplicatedStorage.GameState.Value = "Intermission"
			print(ReplicatedStorage.GameState.Value)
			wait(10)
			ReplicatedStorage.GameState.Value = "InProgress"
			print(ReplicatedStorage.GameState.Value)
		end
		wait(10)
		ReplicatedStorage.GameState.Value = "Waiting for players"
		print(ReplicatedStorage.GameState.Value)
	end
end)

--code here will run
1 Like

these 3 different codes now. which one do i use?

Well any, they’re all different ways of achieving the same task.

after short read about coroutines i think i understand the 1st the most. which one would you use?