MessagingService only passing nils to other servers when values are clearly set in passed table

Hello. I am trying to pass a table that contains Dedicated Server codes. The sorting number is the actual code that the player types in the join the server. This part works fine.

However, If I try passing the same table with the values in it to another server, all of the code values become nil.

Yes, I did test this. I printed the message.Data while indexing every sorting number by doing this:

for num = 100000,100500 do
    print(tostring(num)..": "..tostring(message.Data[num]))
    wait()
end

How I am sending the table:

MessagingService:PublishAsync(MESSAGING_TOPIC, table.pack(servers)) --"servers" being the table that I want to pass.

Am I doing something wrong here? Why is the Subscribing system not picking up any codes?

Full system

Each “lobby” server has a 1 player cap. If I remove this and try the joining system, it works because all of the codes are being stored locally. I want to be able to send new codes whenever a player in a different server creates a server.

local TS = game:GetService("TeleportService")
local DSS = game:GetService("DataStoreService")
local MessagingService = game:GetService("MessagingService")
local MESSAGING_TOPIC = "ServerList"
local Reload_Topic = "Reload"
servers = {}
local ran
local newServerEvent = game.ReplicatedStorage.NewServer
local joinServerEvent = game.ReplicatedStorage.JoinServer
local serverRetrieveEvent = game.ReplicatedStorage.SendServerTable

local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()





--Set table to nil
for num = 100000,100500 do
	servers[num] = nil
end





--//Functions//--
local function CreateNewServer(plr)
	ran = math.random(100000,100500)
	local code = TS:ReserveServer(6660528936)
	table.insert(servers,ran,code)
	print(servers[ran])
	wait(2)
	TS:TeleportToPrivateServer(6660528936,code,{plr},"No Escape - Game",ran)
end





--//Subscriptions//
local subscribeSuccess, subscribeConnection = pcall(function()
	return MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)
		print("Subscribe (recieve): ")
		print(message.Data)

		for num = 100000,100500 do
			print(tostring(num)..": "..tostring(message.Data[num]))
			wait()
			if (servers[num] == nil) and (message.Data[num] ~= nil) then
				servers[num] = message.Data[num]
				print(message.Data[num])
				wait()
			end
		end
	end)
end)
if subscribeSuccess then
	plr.AncestryChanged:Connect(function()
		subscribeConnection:Disconnect()
	end)
end





--Create new server
newServerEvent.OnServerEvent:Connect(function(plr)
	CreateNewServer(plr)
	wait(1)

	local publishSuccess, publishResult = pcall(function()
		MessagingService:PublishAsync(MESSAGING_TOPIC, servers)
	end)
	if not publishSuccess then
		print(publishResult)
	else
		print("Publish (sent): ")
		print(servers)
	end

end)





--Join server via code
joinServerEvent.OnServerEvent:Connect(function(plr,text)
	print(servers[tonumber(text)])
	print(servers[text])
	if servers[tonumber(text)] ~= nil then
		TS:TeleportToPrivateServer(6660528936,servers[tonumber(text)],{plr},"No Escape - Game")
	else
		warn("Server does not exist or has not been loaded.")
	end
end)
2 Likes

I believe the problem is that you are using table.pack. This means that you’re creating an array that has the servers table in its first index and nothing in the other indices. Then, on the other servers, you index this array instead of the servers table that it contains. Can’t you just give the servers table itself to PublishAsync?

I did try that, and when I tried inputting the code from the new table, it didn’t work. However, I did not print the entire server table without the table.unpack so I will do that real quick.

1 Like

Everything is still nil :confused:

(number 10020 being the code. There should not be nil there.)

You had also made a topic where you show that the integer keys were turned into strings. Have you tried using message.Data[tostring(num)] instead of message.Data[num]

for num = 100000,100500 do
	print(tostring(num)..": "..tostring(message.Data[num]))
	wait()
	local messageVal = message.Data[tostring(num)]
	if (servers[num] == nil) and messageVal ~= nil then
		servers[num] = messageVal
		print(messageVal)
		wait()
	end
end
1 Like

Thank you! I thought that was the problem. I just didnt really know how to change it.