Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to filter custom chat?

It's supposed to filter chat

local ChatService = game:GetService("Chat")
 
local chatMessage = game.ReplicatedStorage.ChatMessage
 

local function onChatReceived(sender, message)

	for _, player in pairs(game.Players:GetPlayers()) do
		if sender ~= player then
			local filteredMessage
			if game:GetService("RunService"):IsStudio() then
			
				chatMessage:FireClient(player, sender, message)
			else
				local success, filteredMessage = pcall(function() return ChatService:FilterStringForBroadcast(filteredMessage, sender, player) end)
				if not success then
					filteredMessage = "<Failed to filter message>"
				end
				chatMessage:FireClient(player, sender, filteredMessage)
			end
		end
		end
end
 

chatMessage.OnServerEvent:connect(onChatReceived)
-- ROBLOX Services
local ContextActionService = game:GetService("ContextActionService")
 
-- Static variables
local MAX_MESSAGES = 10
local MESSAGE_HEIGHT = 25
 local NUM_MESSAGE = 0

-- Local variables
local player = game.Players.LocalPlayer
local messages = {}
local chatMessageEvent = game.ReplicatedStorage.ChatMessage
 
-- Variables for GUI elements
local chatScreen = script.Parent
local chatFrame = chatScreen:WaitForChild("ChatFrame")
local chatInput = chatFrame.ChatInput
local messageFrame = chatFrame.MessageFrame
-- Make a copy of the message that will be used later
local messageTemplate = messageFrame.Message:Clone()
messageFrame.Message:Destroy()
 
 
local function addMessage(sender, message)
	-- Check if the number of messages has hit the maximum
	--if #messages >= MAX_MESSAGES then
		-- If so remove the oldest message from the table
	--	table.remove(messages, #messages):Destroy()
	--end
 
	-- Shift all of the messages up one slot

	-- Create new message GUI elements and add to the message table
	if messageFrame:FindFirstChild("LastMessage") then
		messageFrame.LastMessage.Name = "Message"
	end
	local newMessage = messageTemplate:Clone()
	newMessage.NameLabel.Text = sender.Name .. ": "
	newMessage.Content.Text = message
	newMessage.Name = "LastMessage"
	newMessage.Parent = messageFrame
	newMessage.Position = UDim2.new(1, 0, 0, NUM_MESSAGE*MESSAGE_HEIGHT)
	newMessage:TweenPosition(UDim2.new(0, 0, 0, NUM_MESSAGE*MESSAGE_HEIGHT), "Out", "Quad", 0.4,false)
	if newMessage.Position.Y.Offset ==  200 then
	messageFrame.CanvasSize = messageFrame.CanvasSize + UDim2.new(0, 0, 0, 25)
	messageFrame.CanvasPosition = messageFrame.CanvasPosition + Vector2.new(0, 25)
	elseif newMessage.Position.Y.Offset > messageFrame.eh.Position.Y.Offset then
		messageFrame.CanvasSize = messageFrame.CanvasSize + UDim2.new(0, 0, 0, 25) 
		messageFrame.CanvasPosition = messageFrame.CanvasPosition + Vector2.new(0, 25)
	end
	table.insert(messages, 1, newMessage)
	NUM_MESSAGE = NUM_MESSAGE + 1
end
 
-- Function when the input TextBox looses focus
local function onFocusLost(enterPressed, inputObject)
	-- Check if TextBox lost focus because the user pressed "Enter"
	if enterPressed then
		if chatInput.Text ~= "" then
		-- Add the message to the GUI (no need to filter messages from the local player)
		addMessage(player, chatInput.Text)
		-- Send message to the server to get filtered and sent to other players
		chatMessageEvent:FireServer(chatInput.Text)
		-- Reset TextBox text
		chatInput.Text = "Press '/' or click here to chat"
		if game.Players.LocalPlayer:FindFirstChild("Chatting") then
			game.Players.LocalPlayer.Chatting:Destroy()
		end
		else
			chatInput.Text = "Press '/' or click here to chat"
		if game.Players.LocalPlayer:FindFirstChild("Chatting") then
			game.Players.LocalPlayer.Chatting:Destroy()
		end
		end
	elseif  inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		chatInput.Text = "Press '/' or click here to chat"
		if game.Players.LocalPlayer:FindFirstChild("Chatting") then
			game.Players.LocalPlayer.Chatting:Destroy()
		end
	end
end
 
-- Function when the player presses the slash key
local function onSlashPressed(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.End then
		-- If key up then capture focus in the TextBox so the user can start typing
		if game.Players.LocalPlayer:FindFirstChild("Chatting") then
			game.Players.LocalPlayer.Chatting:Destroy()
		end
		local Prev = Instance.new("StringValue",game.Players.LocalPlayer)
		Prev.Name = "Chatting"
		chatInput:CaptureFocus()
	end
end
 
-- Disable ROBLOX default chat
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

 script.Parent.ChatFrame.MessageFrame.AutoSizeCanvas = true

-- Bind functions
chatMessageEvent.OnClientEvent:connect(addMessage)
chatInput.FocusLost:connect(onFocusLost)
ContextActionService:BindAction("Chatting", onSlashPressed, false, Enum.KeyCode.Slash)

3 comments

0

Please, post the code for the event that's being fired.

0

there

1 answer

First off it's FilterStringAsync, FilterStringForBroadcast would only be for the message and the sender, not the all server (Player), and FilterStringAsync was made for chat second, It's if not game:GetService("RunService"):IsStudio() then (Publish it) and try it in Play Mode.

This discussion is preserved as it was. New answers, comments, and votes are closed.