Global chat across all servers

I want to create a global chat script for my obby game, I believe people will get bored if they were by themselves playing it. What kind of script should I implement and is it supposed to be on server script or starterplayerscript, I tried searching in devforum but each person got a different opinion, different script and different place to put it in

Your obby game is splitted in different subPlaces of the same game?
So, players teleports to a different subPlace to continue with that level?

The only way I could imagine to build a chat across all subPlaces would be using MessagingService

But, it has its inconvenients as the limit per publish, and “Delivery is best effort and not guaranteed. Make sure to architect your game so delivery failures are not critical”
image

MessagingService is the way to go. I built one for a single play I help run. You just have to listen for the chatted event on the server, then replicate that out to the other servers, which than post it to the chat. I don’t have access to that code at the moment, but will try to post it later.

1 Like

Alright thank you, lmk when you do

They all start from 0, everytime they join the obby will always be different than the first one they played, it doesn’t save

See below. I found this on either DevForum or YT somwhere. My notes say “GlobalChat.lua, by ForcyDorcy”. I have modified it to add a few extas to it.

--SERVER SCRIPT
-- SERVICES
local Chat = game:GetService("Chat")
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- VARIABLES
-- Player Remote Spamming Tracking
local playerTable = {} -- create an empty table to store an index for everyone in
local cooldownTime = 1 -- Player can only send 1 chat per second

-- FUNCTIONS
function PublishAsync(Data)
	local Success, Result = pcall(function()
		MessagingService:PublishAsync("Chat", Data)
		--print("3 BCAST:", Data)
	end)
end

-- PLAYER JOINS 
function OnPlayerAdded(player)
-- Add Player to 'playerTable' to track Remote Spamming
	if not playerTable[player.Name] then -- if there isn't an entry for the player already
		playerTable[player.Name] = tick() -- Create a new entry in the table for each new player, and make the index their name so we can access it later
	end
	
-- Add Chatted Connect Even	
	player.Chatted:Connect(function(Message)
		
-- Player Spamming Check
		if tick() - playerTable[player.Name] < cooldownTime then 
			return
		end
		playerTable[player.Name] = tick() -- set it back to tick() so it resets
		
-- Filter and Send Chat Message
		local FilteredText = Chat:FilterStringForBroadcast(Message, player)
		local Data = {player.Name, FilteredText}
		local EncodedData = HttpService:JSONEncode(Data)
		PublishAsync(EncodedData)
		--print("4 SENT:", Data)
	end)
end

function OnMessageAsync(Data)
	--print("1 REPL:", Data)
	--print("1 REPL:", HttpService:JSONEncode(Data))
	--ReplicatedStorage.GlobalChat:FireAllClients(Data[1], Data[2])
	ReplicatedStorage.GlobalChat:FireAllClients(Data)
end

-- CONNECTS
Players.PlayerAdded:Connect(OnPlayerAdded)
for _, Player in pairs(Players:GetPlayers()) do
	spawn(function()
		OnPlayerAdded(Player)
	end)
end


-- PLAYER LEAVES - Remove Player from 'playerTable' to track Remote Spamming
game.Players.PlayerRemoving:Connect(function(player)
	if playerTable[player.Name] then -- if there's an entry of the player in the table
		playerTable[player.Name] = nil -- remove it from the table
	end
end)


-- MESSAGIMG SERVICE
MessagingService:SubscribeAsync("Chat", function(Message)
	OnMessageAsync(Message.Data)
	--print("2 CHAT:", Message.Data)
end)
--CLIENT SCRIPT IN STARTER PLAYER
-- SERVICES
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local TextChatService = game:GetService("TextChatService")

-- CHAT WINDOW
local chatWindowConfiguration = TextChatService:FindFirstChildOfClass("ChatWindowConfiguration")
local chatInputBarConfiguration = TextChatService:FindFirstChildOfClass("ChatInputBarConfiguration")

if chatWindowConfiguration then
	chatWindowConfiguration.Enabled = true
end
if chatInputBarConfiguration then
	chatInputBarConfiguration.Enabled = true
end


-- REMOTES
local GlobalChat = ReplicatedStorage:WaitForChild("GlobalChat")

-- VARIABLES
local player = Players.LocalPlayer
local playerTable = {} -- create an empty table to store an index for everyone in

-- FUNCTIONS
function OnGlobalChat(Data)
	if not Data then return end
	-- Format Data	
	Data = string.gsub(Data, "%[", "")
	Data = string.gsub(Data, "%]", "")
	Data = string.gsub(Data, "\"", "")
	local chatData = string.split(Data, ",")

	local Name = chatData[1]
	local Message = chatData[2]

	-- Return if no Name, Message or it is Local Player chat	
	if not Name then return end
	if Name == player.Name then return end
	if not Message then return end

	-- Show Message in Chat
	StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[" .. Name .. "]: " .. Message,
		Font = Enum.Font.SourceSansBold,
	})
end

--EVENTS
GlobalChat.OnClientEvent:Connect(OnGlobalChat)

I’ve removed some of my mods like chat tags and player chat colours. I can’t remember why I had to do the string.gsub might well have been to do with adding chat tags, so might not be required. You will need to experiment with it but the core functions hsould work.

2 Likes