GUI with Server's player amount

I am currently trying to make a GUI that shows how many players are in the server.

My code is:

local colorSoon = Color3.new(1, .5, .5) -- red
local colorDone = Color3.new(.5, 1, .5) -- green
local Players = game:GetService("Players")
local AmountOfPlayers = #Players:GetPlayers()
local tool = script.Parent
local GUI = script.Parent.Part.SurfaceGui

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers + 1
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers - 1
end)

GUI.Frame.TextLabel.Text = AmountOfPlayers
if AmountOfPlayers > 1 then
	GUI.Frame.TextLabel.TextColor3 = colorDone
end

Currently, it shows 2 for my buddy and 1 for me, but there are 3 people in the game.
I believe the problem is that it doesn’t update each time someone joins, it just shows you the number of players there are when you join. I cannot find a way to fix it.

1 Like

I would recommend starting off by setting the AmountOfPlayers variable to 0. The server will start before the play enters. Add on to that variable from there. Make sure you are using a server script, not a local script.

It looks like you are using a local script. You will need to use remote events.

You should be doing that in a loop. I don’t know how is it working but it shouldn’t.

local colorDone = Color3.new(.5, 1, .5) -- green
local Players = game:GetService("Players")
local AmountOfPlayers = nil
local tool = script.Parent
local GUI = script.Parent.Part.SurfaceGui

while true do
 AmountOfPlayers = #Players:GetPlayers()
 gui.Frame.TextLabel.Text = AmountOfPlayers 
  if AmountOfPlayers > 1 then
	 GUI.Frame.TextLabel.TextColor3 = colorDone
         break
  end
wait()
end
1 Like

Hi!

This should work :

local colorSoon = Color3.new(1, .5, .5) -- red
local colorDone = Color3.new(.5, 1, .5) -- green
local Players = game:GetService("Players")
local tool = script.Parent
local GUI = script.Parent.Part.SurfaceGui

local function updatePlayers()
    GUI.Frame.TextLabel.Text = #game.Players:GetChildren()
end
updatePlayers()
game.Players.PlayerAdded:connect(updatePlayers)
game.Playesr.PlayerRemoving:connect(updatePlayers)
1 Like

hello there i am sorry for being not destined to be here when you first created this but the code doesn’t count yourself and the amount of players are only added once for each join and removing vice versa.

you should create a function that will fire everytime a player joins and leaves which you have and to summarize it quicker, i’ll write a code that you can use as such for an example, try to understand it very well and memorize so you may not have to struggle on the similar situations again

--/PROLLY IN THE PART ITSELF YOU SUSSY BAK!!

local players = game:GetService("Players")

local PlrPro = script.Parent
local Projector = PlrPro:WaitForChild("Projector")
local TextGUI = Projector:WaitForChild("DisplayGUI").PlayerText

players.PlayerAdded:Connect(function(plr)
	if plr then
		local amount = #players:GetPlayers()
		print(amount)
		if amount <= 1 then
			TextGUI.Text = "There is currently only "..amount.." player in this server."
		else
			TextGUI.Text = "There are currently "..amount.." players in this server."
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	if plr then
		local amount = #players:GetPlayers()
		print(amount)
		if amount <= 1 then
			TextGUI.Text = "There is currently only "..amount.." player in this server."
		else
			TextGUI.Text = "There are currently "..amount.." players in this server."
		end
	end
end)

I made my own player projector model for this so you can use it if you want