MessagingService Issue

I made a global store using MessagingService, however, the SubscribeAsync gives a really weird error image
I have no idea why this happens, it seems like a bug.

Publishing;

local publishSuccess, publishResult = pcall(function() 
	local data = {
		stock = localSoldItems, --this is a table {key = value}
		jobId = game.JobId
	}
	messagingService:PublishAsync('StockTrader', data)
end)		

Subscribing;

function Subscribe(data) --error appears when calling this function, won't execute anything
	if traderLoaded then --Boolean, set to true
		local newData = data.Data	
		if newData.jobId ~= game.JobId then
			UpdateStock(newData.stock) --ignore this as it won't even execute
		end
	end	
end

messagingService:SubscribeAsync('StockTrader',Subscribe)

If anyone knows what’s going on, please help

1 Like

From what you’re showing us, this is the only time you’re calling anything.
Do you create UpdateStock with coroutine.create?

I’ve never heard of this error before, and a Google search didn’t return any useful results.

I even wrote this piece of code which is heavily based of off your example, and it worked fine, printing 7 as it should do.

local messagingService = game:GetService("MessagingService")

function Subscribe(data)
	print("omg")
	print(data.Data.stock) -- returns 7, as expected
end

messagingService:SubscribeAsync('StockTrader',Subscribe)

wait(1)

local publishSuccess, publishResult = pcall(function() 
	local data = {
		stock = 7, --this is a table {key = value}
		jobId = game.JobId
	}
	messagingService:PublishAsync('StockTrader', data)
end)	

Maybe it’s something to do with where the functions are placed? Or it must be something else - are you sure UpdateStock isn’t running at all?

not it’s a normal function

local function UpdateStock(data) 
	for key,value in pairs(data) do
		if traderStock[key] then	
			traderStock[key] = traderStock[key] + value
			if traderStock[key] <= 0 then
				traderStock[key] = nil
			end	
		else
			if value > 0 then
				traderStock[key] = value
			end		
		end				
	end	
	utilities.directories.Traffic.StockUpdate:FireAllClients(traderStock)
end

Multiple server use Publish Async to this, but that shouldn’t be a problem?

I used prints to see if it would, and it doesn’t.
When that error shows, it won’t print anything

The error “attempt to call a thread value” comes from coroutines. If you’re wrapping the function in coroutine and passing it to SubscribeAsync or are overwriting it somewhere, you’ll get this. The strange thing is that the code you provided shouldn’t be throwing this error.

This is definitely implementation-specific. Do you per chance have conflicting variables? Do other uses of MessagingService work for you?

This is the only one I use, no conflicting values, nothing

Wrapping the function in a Spawn() fixed the issue strange enough…