Player Count Leaderstats

I want to have a leaderstats that displays how much players are in the server, I can’t find a topic similiar to what I’m asking for so if you know a way or know of a topic it would be appreciated.

1 Like

Isn’t it just #game.Players:GetPlayers()?

1 Like
local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player : Player)

    local PlayerCount = #Players:GetPlayers()

    local leaderstats = Instance.new(“Folder”)
    leaderstats.Parent = player
    leaderstats.Name = “leaderstats”


    local TotalPlayers = Instance.new(“IntValue”)
    TotalPlayers.Parent = leaderstats
    TotalPlayers.Name = “Total Players”
    TotalPlayers.Value = PlayerCount
end)
1 Like

Remember not to use Instance.new with a parent argument. Its considered a bad practice because it makes the code much slower.

Do you mean a number at the top of the leaderstats for the amount of players?

this works perfectly, but I already have a leaderstats for currency and both of them can’t work at once so how would I implement them together.

1 Like

just paste that code into the other one

Here:

local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player : Player)

    local PlayerCount = #Players:GetPlayers()

    local leaderstats = player:FindFirstChild("leaderstats")

    local TotalPlayers = Instance.new(“IntValue”)
    TotalPlayers.Parent = leaderstats
    TotalPlayers.Name = “Total Players”
    TotalPlayers.Value = PlayerCount
end)

Use this

local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player : Player)

    local PlayerCount = #Players:GetPlayers()

    local leaderstats = Instance.new(“Folder”)
    leaderstats.Parent = player
    leaderstats.Name = “leaderstats”

    local TotalPlayers = Instance.new(“IntValue”)
    TotalPlayers.Parent = leaderstats
    TotalPlayers.Name = “Total Players”
    TotalPlayers.Value = PlayerCount

	spawn(function()
		while wait() do
			TotalPlayers.Value = #Players:GetPlayers()
		end
	end
end)

What’s also bad practice is you not using the correct quotes characters for strings

There’s no need to use a while loop for this.

I used it so ot changes the value when more players join / leave

hmm still not working. not sure why

1 Like

The PlayerAdded Event detects whenever a player Joins. So each time a player joins/leaves the, the count will be updated.

1 Like

The parent of it is the leaderstats so it will set it for one of the players and not all of them

1 Like

no, for example lets say 2 players joined and a player left the leaderstat will still stay 2

Exactly and different players will have different values aswell

Oh, I see. Never mind then.

2 Likes

this is the original leaderstats, just want to add a player count to that as well.

local Store = DSS:GetDataStore("CashStore1")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Crash"
	cash.Parent = folder
	
	local data
	
	local sucess, err = pcall(function()
		data = Store:GetAsync(player.UserId)
		
		if data and data ~= 0 then
			print("Data sucessfully updated for "..player.DisplayName)
			cash.Value = data
		else
			print(player.DisplayName.." doesn't have any saved data. No Data was updated.")
			cash.Value = 0
		end
	end)
	
	if sucess then
		print("Updating value complete")
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		Store:SetAsync(player.UserId, player.leaderstats.Crash.Value)
	end)
	
	if success then
		print(player.DisplayName.."'s crash data was sucessfully saved!")
	else
		warn(err)
	end
end)
1 Like

Use this instead

local Store = DSS:GetDataStore("CashStore1")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Crash"
	cash.Parent = folder

	local playerCount = game.Players:FindFirstChild("playerCount") or Instance.new("NumberValue")
	playerCount.Name = "playerCount"
	playerCount.Parent = game.Players
	playerCount.Value = #game.Players:GetPlayers()
	
	local data
	
	local sucess, err = pcall(function()
		data = Store:GetAsync(player.UserId)
		
		if data and data ~= 0 then
			print("Data sucessfully updated for "..player.DisplayName)
			cash.Value = data
		else
			print(player.DisplayName.." doesn't have any saved data. No Data was updated.")
			cash.Value = 0
		end
	end)
	
	if sucess then
		print("Updating value complete")
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		Store:SetAsync(player.UserId, player.leaderstats.Crash.Value)
	end)
	
	if success then
		print(player.DisplayName.."'s crash data was sucessfully saved!")
	else
		warn(err)
	end
end)