Lets say there is two servers,
Server A has 5 players. ROBLOX, badcc, Minitoon, Coeptus, Callmehbob.
Server B has another 5 players. Clonetroop1019, mrflimflam, NowDoTheHarlemShake, SamsonXVI, Amanda, and Coefficients
(Yes I had a hard time thinking of 10 popular roblox figures, these are all I could think of)
How can I use messaging service to pick two random players from different servers, say, Amanda and Coeptus?
Yes I know what that is, i just need a way to tackle the task at hand. I want to create a matchmaking system with this, and I was thinking about firing a server event from the client, if it was fired, publishasync, with the player’s name, somehow check anyone from the server also had this event fired, match them together, and teleport? Is this a good way to do this task, or should I handle it differently?
If you are beginner at MessagingService then I recommended start off with simple task such as sending message to another server and print it. Once you done this, slowly increase difficult and get better. Try to start off with simple, I am sure you will become better.
If you disagree above the message then here what you should do :
Bit complex way :
1. Send random player name using publish async
2. Add the player name into table
3. Loop the table and print player name
Simple way : (inspired from someone...)
1. Add all players in to the table (yes, all players in all server)
2. Use math.random to select random player name in the table
EDIT : Please note. I expect you have basic knowledge of scripting and knowing how to optimize thing such as reducing lag, script line, parts in workspace, etc…
for JobId, Server in pairs(Servers) do
DebugPrint("JobId: "..Server.JobId..", Players: "..#Server.Players..", Master: "..(Server.Master and "Y" or "N"))
-- Can access players with 'Server.Players'
end
The server ‘obj’ contains .Players, .IsLocal, .Send(), .Shutdown(), .JobId, .FPS, .Master and .Runtime
The player ‘obj’ contains .Age, .UserId, .Name, .Ping and .Connection
while true do
local ServersTable = {}
for _, Server in pairs(Servers) do
table.insert(ServersTable, Server)
end
if (#ServersTable < 2) wait(10) continue end
local AllPlayersDict = {}
for _, Server in pairs(ServersTable) do
for __, Player in pairs(Server.Players) do
AllPlayersDict[Player.UserId] = Player
end
end
local AllPlayersTable = {}
for _, Player in pairs(AllPlayersDict) do
table.insert(AllPlayersTable, Player)
end
if (#AllPlayersTable) < 2 then wait(10) continue end
local RandomIndex = math.random(1, #AllPlayersTable)
local RandomPlayer1 = AllPlayersTable[RandomIndex1]
table.remove(AllPlayersTable, RandomIndex)
RandomIndex = math.random(1, #AllPlayersTable)
local RandomPlayer2 = AllPlayersTable[RandomIndex1]
table.remove(AllPlayersTable, RandomIndex)
print(RandomPlayer1.Name.." (Ping: "..RandomPlayer1.Ping.." UserId: "..RandomPlayer1.UserId..")")
print(RandomPlayer2.Name.." (Ping: "..RandomPlayer2.Ping.." UserId: "..RandomPlayer2.UserId..")")
break
end
I tostringed it, and now it says Attempt to concatenate string with Instance
local AoQ = game.ReplicatedStorage.AddtoQueue
local RFQ = game.ReplicatedStorage.RemoveFromQueue
local MessageService = game:GetService("MessagingService")
local TeleportService = game:GetService("TeleportService")
local PlayersToMatch = {}
local place = 10262480669
AoQ.OnServerEvent:Connect(function(player)
table.insert(PlayersToMatch, player)
MessageService:PublishAsync("ChosenPlayer", tostring(PlayersToMatch[math.random(#PlayersToMatch)]))
end)
RFQ.OnServerEvent:Connect(function(player)
table.remove(PlayersToMatch, player)
end)
MessageService:SubscribeAsync("ChosenPlayer", function(player)
print("We have chosen"..player.Data.." vs. "..PlayersToMatch[math.random(#PlayersToMatch)])
end)