Ok so I am trying to make a status system where it gets all the players playing my game on all servers and puts them into a ui but how would I go about doing that Ive tried using a datastore but it didnt work. Thank You
I’d probably just use messaging service.
You can send pretty much any data to a server using it, in this case player id’s / names depending on how you want to go about it.
I havent actually tried this so the following code might error, also it won’t work if multiple servers are trying to get players. You could probably fix that with a unique MessageID and be fine but yeah.
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local ServerID = game.JobId
function GetAllPlayersFromRunningServers()
local AllPlayers = {}
local MessagingConnection
local LastMessageRecievedTime = 0
local StartTick = tick()
MessagingConnection = MessagingService:SubscribeAsync("GetAllPlayers", function (PlayerData)
table.insert(AllPlayers, PlayerData)
LastMessageRecievedTime = 0
StartTick = tick()
end)
MessagingService:PublishAsync("SendAllPlayers", {JobID = ServerID})
while task.wait() do
LastMessageRecievedTime += tick() - StartTick
if LastMessageRecievedTime > 3 then
MessagingConnection:Disconnect()
break
end
end
return AllPlayers
end
MessagingService:SubscribeAsync("SendAllPlayers", function(Message) --got message from a server to send all your players over
if Message.JobID == ServerID then -- make sure we dont send our own players (unless you want to)
return
end
local PlayerData = {}
for _, Player: Player in Players:GetPlayers() do
table.insert(PlayerData, {Name = Player.Name; UserID = Player.UserId}) --packing these both into a table just in case you want either or
end
MessagingService:PublishAsync("GetAllPlayers", {PlayerData = PlayerData; ServerID = ServerID}) --
end)
print(GetAllPlayersFromRunningServers())
Why not to use for i loop?
it’s so much easier and less complicated than this