Messagingservice parameters returning nil

I’m trying to print out some data passed via messagingservice, and the prints return nil for both parameters.

why?

local ms = game:GetService("MessagingService")

local idTable = {
	["1"] = 10825692298;
	["2"] = 10825692644;
	["3"] = 10825692775;
}

local function subscribe()
	for i,v in pairs(idTable) do
		ms:SubscribeAsync(tostring(v), function(dataCallback)
			if dataCallback.ID == nil then
				print(dataCallback["ID"], dataCallback["plrNum"])
			end
		end)
	end
end
subscribe()

local function publish()
	for i,v in pairs(idTable) do
		local data = {
			["ID"] = game.GameId;
			["plrNum"] = #game.Players:GetChildren()
		}
		ms:PublishAsync(tostring(v), data)
	end
end

local function cycler()
	publish()
	task.wait(5)
	cycler()
end

cycler()

when getting data from messaging service you have to call .Data from the returning value
So you would call dataCallback.Data.ID instead

Hope this helps

lord, im an idiot. thanks so much, haha.

All good lol confused me a little when I was first learning the service

yeah, im a bit new as well. is there any way to get the number of players within other servers using MessagingService?

i know there is, but idk how to.

i would not use messaging service for that, i would use datastores and make a universal key that a server will append its server id or whatever as the key and the values(s) can be the number of players and so when one server wants to see how many players are in a server you just have to grave the datastore with the key of the server your looking for.
Just make sure to remove the players from the number when a player leaves and to remove the key entirely when a server shuts down

alright, thanks mate. have a good one

1 Like

Thanks good luck on your project

1 Like