Issues passing tables through MessagingService

Hello! I’m currently working on a lobby/team selecting system where you select what team you want to join, and it finds an available server with the selected team available (as they are 1 player teams) via the new MessagingService

I’m having a baffling issue however.

The lobby and game are 2 seperate places within the same game universe.

When the message publishes from the lobby, the game server does receive it, but the data it gets is nil and I have spent many hours looking for ways to fix this, but I am totally stumped at this point.

Lobby code:

local messagingService = game:GetService("MessagingService")
local teleportService = game:GetService("TeleportService")
local test = game.ReplicatedStorage.Test
local findServers = game.ReplicatedStorage.FindServers
local foundServers = false
local teleportGUI = game.ReplicatedStorage.Teleporting
local plrNameVal = script.PlrName
local plrTeamVal = script.PlrTeam
local placeID = 3143415630

function delayEndOfServer()
	wait(20)
	
end

game:BindToClose(delayEndOfServer)

findServers.OnServerEvent:Connect(function(player, team)
	plrNameVal.Value = player.Name
	plrTeamVal.Value = team
	
	local infoToGive = {
		playerName = plrNameVal.Value,
		playerTeam = plrTeamVal.Value
	}
	
	local function joinServer(data, unixTime)
		if data[1] == player.Name and data[2] == team then
			foundServers = true
			teleportService:TeleportToPlaceInstance(placeID, data[3], player, nil, plrTeamVal.Value)
		end
	end
	
	messagingService:SubscribeAsync("ServerFinderListener", joinServer)
	messagingService:PublishAsync("ServerFinderPublisher", infoToGive)
	
	wait(3)
	
	if foundServers == false then
		teleportService:Teleport(placeID, player, plrTeamVal.Value)
	end

end)

Server code:

local teams = game:GetService("Teams")
local msgSvc = game:GetService("MessagingService")
local teleportSvc = game:GetService("TeleportService")
local setTeamData = game.ReplicatedStorage.SetTeamData

game.Players.PlayerRemoving:Connect(function(plr)
	local plrTeam = plr.Team
	plrTeam.Taken.Value = false
end)

setTeamData.OnServerEvent:Connect(function(player, team)
	player.Team = teams[team]
	teams[team].Taken.Value = true
	wait(2)
	player.Character:MoveTo(game.Workspace.Spawns[team].Position)
end)

function checkAvailability(data, unixTime)
	local player = data.playerName 
	print(player)
	if game.Players:FindFirstChild(player) == nil then
		if teams[data.playerTeam].Taken.Value == false then
			local infoToSend = {
				[1] = player,
				[2] = data.playerTeam,
				[3] = game.JobId
				}
		
			msgSvc:PublishAsync("ServerFinderListener", infoToSend)
			infoToSend = nil
		end
	end
end

msgSvc:SubscribeAsync("ServerFinderPublisher", checkAvailability)

The only error happens on line 21 of the serverside code where it checks if the player due to join already exists in the server via the :FindFirstChild() function, but it errors and always says “Arguement 1 missing or nil”. Also printing the players name returns “nil”, so the data being sent is full on nil as the team is nil too.

If anyone can point out what I’m doing wrong here, that would be very helpful. Thanks :smiley:

-reddust1

The callback argument for MessagingService:SubscribeAsync is a dictionary containing the actual data and the time at which the message was sent. Your code was taking the argument as a tuple instead of a dictionary.

Test the code below and see if you need to fix any errors.

Lobby code:

local messagingService = game:GetService("MessagingService")
local teleportService = game:GetService("TeleportService")
local test = game.ReplicatedStorage.Test
local findServers = game.ReplicatedStorage.FindServers
local foundServers = false
local teleportGUI = game.ReplicatedStorage.Teleporting
local plrNameVal = script.PlrName
local plrTeamVal = script.PlrTeam
local placeID = 3143415630

function delayEndOfServer()
	wait(20)
	
end

game:BindToClose(delayEndOfServer)

findServers.OnServerEvent:Connect(function(player, team)
	plrNameVal.Value = player.Name
	plrTeamVal.Value = team
	
	local infoToGive = {
		playerName = plrNameVal.Value,
		playerTeam = plrTeamVal.Value
	}
	
	local function joinServer(data)
		local info = data.Data
		
		if info[1]== player.Name and info[2] == team then
			foundServers = true
			teleportService:TeleportToPlaceInstance(placeID, data[3], player, nil, plrTeamVal.Value)
		end
	end
	
	messagingService:SubscribeAsync("ServerFinderListener", joinServer)
	messagingService:PublishAsync("ServerFinderPublisher", infoToGive)
	
	wait(3)
	
	if foundServers == false then
		teleportService:Teleport(placeID, player, plrTeamVal.Value)
	end

end)

Server code:

local teams = game:GetService("Teams")
local msgSvc = game:GetService("MessagingService")
local teleportSvc = game:GetService("TeleportService")
local setTeamData = game.ReplicatedStorage.SetTeamData

game.Players.PlayerRemoving:Connect(function(plr)
	local plrTeam = plr.Team
	plrTeam.Taken.Value = false
end)

setTeamData.OnServerEvent:Connect(function(player, team)
	player.Team = teams[team]
	teams[team].Taken.Value = true
	wait(2)
	player.Character:MoveTo(game.Workspace.Spawns[team].Position)
end)

function checkAvailability(data)
	local player = data.Data.playerName 
	print(player)
	if game.Players:FindFirstChild(player) == nil then
		if teams[data.playerTeam].Taken.Value == false then
			local infoToSend = {
				[1] = player,
				[2] = data.playerTeam,
				[3] = game.JobId
				}
		
			msgSvc:PublishAsync("ServerFinderListener", infoToSend)
			infoToSend = nil
		end
	end
end

msgSvc:SubscribeAsync("ServerFinderPublisher", checkAvailability)

Ah fantastic, this is working now. Thank you so much! I was completely stumped.