Unable to cast value to function

Hi I’m trying to make a global message and I have no idea how it works I’m pretty much copying code since the documentation won’t load for some reason, but it’s saying unable to cast value to function. Here is my code. Please help.
1.

local MarketplaceService = game:GetService("MarketplaceService")
local Player = game:GetService("Players").LocalPlayer

local PlayerID = Player.UserId
local GamepassID = 1540306689

local Message = script.Parent.Parent.Message.Text

script.Parent.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(Player, 1540306689)
	MarketplaceService.PromptProductPurchaseFinished:Connect(function(Player, Gamepass, IsPurchased)
		if IsPurchased == true then
			script.Parent.Server:FireServer(Player, Message)
		end
	end)
end)
local MessagingService = game:GetService("MessagingService")

script.Parent.Server.OnServerEvent:Connect(function(Player, Message)
		-- send a global message of the message carried on by the function.  This is a test.
		local text = game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage", {
			Text = Message,
			Color = Color3.fromRGB(255, 0, 0),
			Font = Enum.Font.SourceSansBold,
			TextSize = 18
		})
	MessagingService:SubscribeAsync("Topic", Message)
	MessagingService:PublishAsync("Topic", Message)
end)

:SubscribeAsync needs 2 parameters and #1 is a string and #2 is a function. in your code you gave “Topic” and something seems to be a string variable named “Message”

I’m not sure what function I should use. I tried changing it to message, topic but that didn’t work

You do not need to send the player to the server, the server will already have it by default once the event fires. Change the line to:

script.Parent.Server:FireServer(Message)

Do not put events into other events without disconnecting the inner-event. If the button is clicked more than once, additional PromptProductPurchseFinished events will fire, causing a memory leak and degrading performance. Move the Finished event outside of the MouseButton1Click event.

The second argument of SubscribeAsync takes a function (it’s a callback).

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

And like the aforementioned previous connection, move SubscribeAsync outside of the OnServerEvent connection.

Ok so I did the things you said but it doesn’t send a message in chat. Here is data:

["Data"] = "",
["Sent"] = --"A bunch of Numbers"--

And also I’m curious how does the IsPurchased boolean available if it isn’t in the function?

This is my new code:

local MarketplaceService = game:GetService("MarketplaceService")
local Player = game:GetService("Players").LocalPlayer

local PlayerID = Player.UserId
local GamepassID = 1540306689

local Message = script.Parent.Parent.Message.Text

script.Parent.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(Player, 1540306689)
	MarketplaceService.PromptProductPurchaseFinished:Connect(function(Player, Gamepass, IsPurchased)
		local text = game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage", {
			Text = Message,
			Color = Color3.fromRGB(255, 0, 0),
			Font = Enum.Font.SourceSansBold,
			TextSize = 18
		})
	end)
	if IsPurchased == true then
		script.Parent.Server:FireServer(Message)
	end
end)

And

local MessagingService = game:GetService("MessagingService")
MessagingService:SubscribeAsync("Topic", function(data)
	print(data)
end)
script.Parent.Server.OnServerEvent:Connect(function(Player, Message)
	MessagingService:PublishAsync("Topic", Message)
end)

Your scripts are good for the most part:

local MarketplaceService = game:GetService("MarketplaceService")
local Player = game:GetService("Players").LocalPlayer

local PlayerID = Player.UserId
local GamepassID = 1540306689

local Message = script.Parent.Parent.Message

MarketplaceService.PromptProductPurchaseFinished:Connect(function(Player, Gamepass, IsPurchased)
		local text = game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage", {
			Text = Message.Text,
			Color = Color3.fromRGB(255, 0, 0),
			Font = Enum.Font.SourceSansBold,
			TextSize = 18
		})
	
     if IsPurchased == true then
		script.Parent.Server:FireServer(Message.Text) -- add .Text here to get the updated message
	 end
end)

script.Parent.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(Player, 1540306689)
end)

MessagingService:SubscribeAsync("Topic", function(data)
	print(data.Data) -- print the message content
end)

Now it printed “Test” at the print data.data part but it still isn’t sending anything in chat. Also, message already is a .text of a textbutton. When I add the “.text” it sends an error, saying .text is nil. Also, the print data.data is sending " - " now, so it must be nil somehow.

If Message is a TextBox, don’t add .Text to the initial variable, but if it isn’t, then you can switch those changes back (I assumed Message was a TextBox).

If you’re using the new Roblox chat, you need to switch it back to the legacy chat via TextChatService to use the StarterGui:SetCore method

I did both of those things but it still isn’t sending anything in chat. I’m stumped what to do I even asked chatgpt too.