You would need to do this, I suggest you use my code over @OIogist since it is more organized
local PlayersService = game:GetService("Players")
local txt = script.Parent
local function UpdatePlayerCount()
txt.Text = "Players: ".. #PlayersService:GetPlayers() .."."
end
UpdatePlayerCount()
PlayersService.PlayerAdded:Connect(UpdatePlayerCount)
PlayersService.PlayerRemoving:Connect(UpdatePlayerCount)
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)
You may have to wrap your while loop in a coroutine because at the look of your code it might be yielding the script preventing the code below to be reached
local Players = game:GetService("Players")
local function updatePlayerCount()
for i, v in pairs(game.Players:GetPlayers()) do
return i
end
end
game.Players.ChildAdded:Connect(function()
local count = "Players: " .. updatePlayerCount()
script.Parent.textLabel.Text = count
end)
game.Players.ChildRemoved:Connect(function()
local count = "Players: " .. updatePlayerCount()
script.Parent.textLabel.Text = count
end)
This method is not efficient, because what if 2-3 people leave and it takes 1 away, I suggest just using childs & updating them for when you need too as your way is inefficient.
You should loop it through every single player in the game,
for i (index), v (value) in pairs(game.Players:GetPlayers()) do
Index is the number you’re getting, think it as a route so you have like 50 routes inside of 1 folder, you would do.
for i, v in pairs(game.Workspace.Routes:GetChildren()) do
print("Route"..i) -- It will print the route name
end
So you should loop it through every player inside of the game, this script works on client/server.
local Players = game:GetService("Players")
local function updatePlayerCount()
for i, v in pairs(game.Players:GetPlayers()) do
return i
end
end
game.Players.ChildAdded:Connect(function()
local count = "Players: " .. updatePlayerCount()
script.Parent.textLabel.Text = count
end)
game.Players.ChildRemoved:Connect(function()
local count = "Players: " .. updatePlayerCount()
script.Parent.textLabel.Text = count
end)
local RunService = game:GetService("RunService")
local txt = script.Parent
local players = game:GetService("Players")
RunService.Stepped:Connect(function()
txt.Text = "Current Players: "..#players:GetPlayers()
end)
-- // Goodluck on your developing!
local PlayerService = game:GetService("Players")
local count = PlayerService:GetPlayers()
PlayerService.PlayerAdded:Connect(function()
txt.Text = "There is "..#count.." players in this server."
end)
PlayerService.PlayerRemovedConnect(function()
txt.Text = "There is "..#count.." players in this server."
end)
PlayerRemoving will fire whenever a player instance leaves the game, even if three players leave at the same time the PlayerRemoving event will fire for each, remember that it’s just firing whenever a player instance is removed from the PlayersService, it’s essentially the same as the following.
game.Players.ChildRemoved:Connect(function(Child)
--child is a player instance
end)
A easy way to achieve something like this is to track when players are added and removed from the game and update the billboard accordingly. I’ve wrote some code to show how this could be done:
local Players = game:GetService("Players")
local part = workspace.Part -- The part which the BillboardGui is in
-- Updates the BillboardGui to display the current amount of players in the game
local function updateDisplay()
part.BillboardGui.TextLabel.Text = "Players: " .. #Players:GetPlayers()
end
local function onPlayerAdded(player)
-- Run the updateDisplay function because a player has joined the game
updateDisplay()
end
local function onPlayerRemoving(player)
-- Run the updateDisplay function because a player has left the game
updateDisplay()
end
-- Handle player added
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
onPlayerAdded(player)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- Handle player removing
Players.PlayerRemoving:Connect(onPlayerRemoving)
When making something like this I’d avoid using while loops or heartbeat loops to track how many players are in the game and opt to use PlayerAdded and PlayerRemoving. It is generally better practice to use events if they exist instead of while loops.
local Players = game:GetService("Players")
playerCount = 0
local function updatePlayerCount(player)
local players = Players:GetPlayers()
playerCount = #players
end
coroutine.resume(coroutine.create(function()
while true do
repeat
print("players: "..playerCount)
wait(1)
until playerCount >= 1
wait(5)
end
end))
Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)