How can I make the gui textlabel show how many players are on the server in a certain game?
try with this
while wait() do
script.Parent.Text = "PLAYERS: "..game.Workspace["Main"].Players.Value
end
Iâd recommend you connect to when the players join and disconnect and then use :GetPlayers() to return you a table of the players connected into the game. Then all you have to do is find the length of the table. e.g.
game.Players.PlayerAdded:Connect(function(player)
script.Parent.Text = #game.Players:GetPlayers()
end)
local players = game:GetService("Players")
local label = script.Parent
local function onPlayerCountChanged()
label.Text = #players:GetPlayers()
end
players.PlayerAdded:Connect(onPlayerCountChanged)
players.PlayerRemoving:Connect(onPlayerCountChanged)
onPlayerCountChanged()
I need the GUI plate to say how many players are in my other place! how to do it
I need the GUI plate to say how many players are in my other place! how to do it?
You mean another game? Or another place linked to your game? I feel like messagingservice would be good for linked placed but for another game you have to use a proxy.
another place related to my game!
In that case, use MessagingService. Create a script in your place and use MessagingService to tell all servers that a player has joined. This might help too.
OK, but can you demonstrate exactly what I should do where to put the script will it be a module?
This is a bit of a complicated answer.
What game? Your own or another persons? If itâs your own game, that makes things slightly simpler. If itâs another persons game that youâre looking to get the player count of, then you can forget it as it will require a server / proxy.
In the case that itâs your own game, use MessagingService.
MessagingService allows all the servers in your game to communicate with each other. For example, doing MessagingService:PublishAsync("PlayerCount", 1) will tell all servers that a player has been added to the game. And doing MessagingService:SubscribeAsync("PlayerCount", handler) will invoke âhandlerâ with a table containing a timestamp and the data you sent through (data.Data and data.Sent).
So how do you accomplish this? Letâs see.
- You need to know when a server joins (and leaves)
- You need to have a count kept everywhere.
First one is pretty simple, as demonstrated before. But that last one will pose a problem for us, who keeps the count? The solution is to have every server keep a count and then send that value back when the count is needed.
local count = 1
local countInformed = false
local REQUEST_TYPES = {
SET = {
Key = "SET",
Func = function(data, sent)
if (data:sub(1, 1) == "+" and tonumber(data:sub(2))) then
-- If we're adding to the count, then:
count += tonumber(data:sub(2))
elseif (data:sub(1, 1) == "-" and tonumber(data:sub(2))) then
-- If we're decrementing the count, then:
count -= tonumber(data:sub(2))
elseif (tonumber(data)) then
count = tonumber(data)
end
end
GET = {
Key = "GET",
Func = function(data, sent)
if (data.Asking and (not countInformed)) then
if (not countInformed) then
return nil
end
-- We have gotten the count before! we can confidently give it back
return count, {ASKING = false}
end
count = math.max(count, data.Count) -- Choose whichever one is larger
end
}
MessagingService:SubscribeAsync("PlayersCount", function(data)
local meta, sent = data.Data, data.Sent -- Some info about our info, and timestamp of when sent.
if (meta.JOBID == game.JobId) then
return nil
end
local data = meta.DATA -- Our actual data
local ret, publishTab = REQUEST_TYPES[meta.REQUEST_TYPE].Func(data, sent)
if (ret) then
publishTab.REQUEST_TYPE = meta.REQUEST_TYPE
publishTab.JOBID = game.JobId
publishTab.DATA = ret
MessagingService:PublishAsync("PlayersCount", publishTab)
end
end)
MessagingService:PublishAsync("PlayersCount", {
REQUEST_TYPE = REQUEST_TYPES.GET.KEY, -- Request type, we're GETting.
JOBID = game.JobId, -- Unique identifier
ASKING = true, -- Whether or not we're asking over sending the count
DATA = {Asking = true}
)
task.spawn(function()
task.wait(10)
if (not countInformed) then
-- No one is replying, we must be the first server
countInformed = true
end
end)
Players.PlayerAdded:Connect(function()
MessagingService:PublishAsync("PlayersCount", {
REQUEST_TYPE = REQUEST_TYPES.SET.KEY,
JOBID = game.JobId,
DATA = "+1"
})
end)
Players.PlayerRemoving:Connect(function()
MessagingService:PublishAsync("PlayersCount", {
REQUEST_TYPE = REQUEST_TYPES.SET.KEY,
JOBID = game.JobId,
DATA = "-1"
})
end)
This system is NOT perfect. Itâs untested and not formally constructed at all. But in general this is what youâre looking for. Some things that my system is missing are:
- Proper GET/SET queuing for exact counting
- Error prevention / handlers
- Multi-count sending
- Testing
- Cache sending
But in general, thatâs the concept. My code will only give a rough player count due to the nature of how it handles the data.
Hope this helps, MessagingService can be complicated since you pretty much have to deal with it in the same way that a server deals with requests.
I do apologise if this isnât helpful, I havenât done this stuff in a long time.
Nothing really, I want to make it so that in the main game the plate shows how many players per server, here is an example, there is a join button and on the left there is a plate of 30/40 players like this
I was going based off your previous reply.
Your case is kind of specific to your game and its current scripting. But if youâre looking for a general count then do #game:GetService("Players"):GetPlayers().
Where can I put this script to make it work?
Put the script inside ServerScriptService
To get the number of current players inside a single server, put this in a LocalScript under your GUI:
while wait(0.05) do -- Not very good for performance, however, it'll update every 0.05 seconds.
script.Parent.Text = #game:GetService("Players"):GetPlayers() - 1 -- If you don't want it to count you.
end
local Game = game
local Players = Game:GetService("Players")
while true do
print(#Players:GetPlayers().." number of players.")
Players.PlayerAdded:Wait()
end
Iâd advise against polling.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.