I made a global store using MessagingService, however, the SubscribeAsync gives a really weird error
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)
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?
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?
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?