Can't access table values

I’m trying to send some data across all servers using MessagingService, but I cannot access it.

Basically, I want to send some data when stepped on a part:

local MessagingService = game:GetService("MessagingService")

local pickup = script.Parent

pickup.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then	
			local player = game.Players:FindFirstChild(part.Parent.Name)		
			local data = {player.Name, pickup.Parent.Name}
			messagingService:PublishAsync("Purchase", data)		
		end
	end
end)

If I use just a print table statement:

MessagingService:SubscribeAsync("Purchase", function(data)
	print(data)
end)

I get the data that I need:
Untitled

But if I try to access the values (I also tried unpacking the table):

MessagingService:SubscribeAsync("Purchase", function(data)
	print(data[1])
end)

The print statement is nil. How can I access the values?

the data argument in the callback function basically consists of 2 tables of having keys Data and Sent. If u are trying to access the Data, u have to access it by Key instead of index

MessagingService:SubscribeAsync("Purchase", function(data)
	print(data.Data) --now u can access the data table elements by index 
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.