Showing Player In Server Count on a part

I want to show the amount of players on a surface gui on a part,

image

Directory

image

here’s the script I used :

local sp = script.Parent
local Players = game:GetService("Players")
 
    game.Players.PlayerAdded:Connect(function(player)
     
Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do

	 sp.Value=sp.Value+1
	sp.Text=sp.count.Value
end
end)

For some reason it doesn’t change the text on the gui , is there an any error with my code?
—any help will be appreciated.
**edit never mind i just found out i was referring to the parent instead of text. this code worked

local sp = script.Parent
local Players = game:GetService("Players")
 
    game.Players.PlayerAdded:Connect(function(player)
     
Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do

	 sp.count.Value=sp.count.Value+1
	sp.Text=sp.count.Value
end
end)

A simpler way to do this is with #Players:GetPlayers() which will return the amount of players in the game.

2 Likes

was referring to the parent as text,didnt notice lol, next time i’ll be more careful

I belive you’re not incrementing the actual value, as you’ve messed up variables.

A simple fix is this:

local Players = game:GetService("Players")
 
    game.Players.PlayerAdded:Connect(function(player)
     
Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do

	 sp.Value = sp.count.Value+1
	sp.Text = sp.count.Value
end
end)```

However, you may want to loop this so that it updates whenever a player joins/leaves, as right now it fires asap.

but how would we use #players:getPlayer() to actually show the value

You could use it like this:

local Players = game:GetService("Players")
 
Players.PlayerAdded:Connect(function()
	TextLabel.Text = #Players:GetPlayers()
end)

Players.PlayerRemoving:Connect(function()
	TextLabel.Text = #Players:GetPlayers()
end)
1 Like

Well can you please show your explorer then? I’m confused to the order.