You can write your topic however you want, but you need to answer these questions:
get players in all servers
list will return “no data” at first
yes
so basically i want to like get players in all servers via a ‘nwplayers’ command inside the mainmodule of basic admin, but the list returns no data until i refresh it
local function GetPlayers(JobId)
local AllPlayers = {}
local MessageService = game:GetService('MessagingService')
MessageService:PublishAsync("GetPlayers",JobId)
MessageService:SubscribeAsync("GetPlayers", function(GameId)
for a,b in next, game.Players:GetPlayers() do
if GameId == game.JobId then
table.insert(AllPlayers, b.Name.. " [In current server]")
else
table.insert(AllPlayers, b.Name.. " [Not in current server] ("..game.JobId..")")
end
end
end)
return AllPlayers
end
function Funcs.GetAllUsers(Args)
local Player = Args[1]
local Result = GetPlayers(game.JobId)
essentialsEvent:FireClient(Player, 'List','Nationwide Players',true,true,Result)
end
Presumably because the subscription is published and subscribed in the same function, hence when you run the command for the first time, the server first publishes the data and then connects the subscription event listener to your function in later iterations. Taking the listener function outside of GetPlayers() should suffice.
Another reason to also do that is because you do not want to declare a new subscription each time the command is ran, and running the command more than once will result in the subscription connecting more than once, hence the function stacking with multiple uses, in turn contributing to a potential memory leak.
Lastly, SubscribeAsync returns an RBXScriptConnection Instance containing a table which holds your parsed data (not the data itself directly). Hence, your if GameId == game.JobId then condition will never be true. Instead, compare the Data element of the GameId Instance instead.
if GameId.Data == game.JobId then
-- Do whatever you need to do
end