Cross Server 'texting' help

I’d like to create a cross-server chat system (using GUIS, not the chat bar)

I have it so that every message a player sends is filtered, and then published using messagingservice to all servers. In the console, it prints the message someone has sent but doesn’t run the success function - I have no idea why.

Server script code:

Code
local MessagingService = game:GetService("MessagingService")
local TextService = game:GetService("TextService")

local SendEvent = game.ReplicatedStorage:WaitForChild("Send")

local MessageTopic = "Text"


function Filter(msg, User)
	local result
	
	local success, err = pcall(function()
		result = TextService:FilterStringAsync(msg, User)
	end)
	if success then
		print("FIRST WOOP LOL")
		return result
	end
	return false
end

function GetFiltered(Text, Reciever)
	local result
	local success, err = pcall(function()
		result = Text:GetChatForUserAsync(Reciever)
	end)
	if success then
		print("WOOP")
		return result
	end
	return false
end

function OnSendMsg(player, message)
	local ReturningValue = false
	if message ~= "" then
		local filtered = Filter(message, player.UserId)
		
		if filtered then
			for _, player in pairs(game.Players:GetPlayers()) do
				local FilteredMessage = GetFiltered(filtered, player.UserId)
				ReturningValue = FilteredMessage
			end
		end
			
	end
	return ReturningValue
end


game.Players.PlayerAdded:Connect(function(Player)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	local Hum = Char:WaitForChild("Humanoid")
	
	local PlayerGui = Player:WaitForChild("PlayerGui")
	local TextGui = PlayerGui:WaitForChild("CrossServerTest")
	local RecievingText = TextGui:WaitForChild("Recieving")
	
	local MessageRecieving
	local MsgSender
	
	local subscribeSuccess, subscribeConnection = pcall(function()
		return MessagingService:SubscribeAsync(MessageTopic, function(message)
			print(message.Data)
			print("INNIT HERE")
			--MessageRecieving = message.Data
		end)
	end)
	if subscribeSuccess then
		print(MessageRecieving)
		--local stringsplit = string.split(MessageRecieving, "kill/")
		--if stringsplit[2] == Player.Name then
		--	Hum.Health = Hum.Health - 10
		--else
			RecievingText.Text = MessageRecieving
		--end
	end
end)

SendEvent.OnServerEvent:Connect(function(Player, Text)
	local PlayerGui = Player:WaitForChild("PlayerGui")
	local TextGui = PlayerGui:WaitForChild("CrossServerTest")
	local SendingText = TextGui:WaitForChild("Sending")
	local FilteredMessage = OnSendMsg(Player, Text)
	if FilteredMessage ~= false then
		print("yaa")
		SendEvent:FireClient(Player, FilteredMessage)
		local publishsuccess, publisherr = pcall(function()
			MessagingService:PublishAsync(MessageTopic, FilteredMessage)
		end)
		if publishsuccess then
			print("Success")
		else
			warn(publisherr)
		end
	end
end)

Please recommend any good dev-forum tutorials I could use, I’ve looked at the API but it doesn’t explain what ‘message’ contains besides Data, and doesn’t explain the other things I need to know.

what is the success function in the script?

Here’s the help with MessagingService that I recived: click me. You can also use Events to make Local Chat Message.

Get message Script example:

local MessageEvent = game:GetService("ReplicatedStorage"):WaitForChild("MessageE")
game:GetService("MessagingService"):SubscribeAsync("CrossServerChatMessage", function(message)
MessageEvent:FireAllClients(message.Data)
end)

Local Script example:

local MessageEvent = game:GetService("ReplicatedStorage"):WaitForChild("MessageE")
MessageEvent.OnClientEvent:Connect(function(data)
-- Code to prompt a chat message.
end)
1 Like

What do you mean? It’s a pcall, and something runs if it successfully gets the text.

Hmm, but the problem is with the script not successfully recieving the message - it never runs the success function.