How to add a filter to a command?

  1. What do you want to achieve?
    A “/setmessage” command that has filtering.
  2. What is the issue?
    There’s no issue here, I just don’t know how to filter the command. I said what I tried in “What solutions have you tried so far?”
  3. What solutions have you tried so far?
    Tried to filter table “args”, didn’t work.

Code:

-- [ STARTING VARIABLES ] --

local commands = {}

local prefix = "/" 

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("Setmessage")

local Remote2 = RS:WaitForChild("SetmessageOFF")

local TextService = game:GetService("TextService")

-- [ MAIN CODE ] --

-- Setmessage Command --

commands.setmessage = function(sender, args) -- sender: Object - args : Table
	-- What will happen when you run the command.
	print("Setmessage command ran by:")
	print(sender)
	
	for i, playerName in pairs(args) do
		print(playerName)
	end
	
	local MessageToSet = table.concat(args, " ")
	
	Remote:FireAllClients(MessageToSet)
	
	print("fired event")
 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		if player.Name == "airsoft561" then
		
		local splitString = message:split(" ") -- {":setmessage","message"}
		
		local prefixCmd = splitString[1] -- ":setmessage"
		
		local cmd = prefixCmd:split(prefix) -- {":","setmessage"} -- another table
		
		local cmdName = string.lower(cmd[2])
		
		
		if commands[cmdName] then
			
			local args = {} -- Other splited, eg: "yeet" if you did: ":setmessage yeet"
			
			for i = 2, #splitString, 1 do
				table.insert(args,splitString[i])
			end
			
			commands[cmdName](player,args) -- Fires function
				
			end
		else
			print("Player not authorized.")
		end
		-- ":setmessage Interviews" ----> {":setmessage","Interviews"} table
	end)
end)

-- Setmessage OFF Command --

Use the TextService FilterStringAsync function, this will return a TextFilterResult. For this to become a string, you can either use three functions:

  1. TextFilterResult:GetChatForUserAsync(toPlayerUserId)
  2. TextFilterResult:GetNonChatStringForBroadcastAsync()
  3. TextFilterResult:GetNonChatStringForUserAsync(toPlayerUserId)

These functions returns the filtered string.

For more info about how this, refer to this dev hub page: Text Filtering | Documentation - Roblox Creator Hub.

Where would I put this in my code?

1 Like

It should be used when you are gonna filter a string.

In your case, MessageToSet is what you should filter.

local StringFilterResult = TextService:FilterStringAsync(MessageToSet)
local FilteredString = StringFilterResult:[AnyOfTheFunctions_I_Stated]
print(FilteredString)

Please do remember to wrap it in a pcall.

Wdym by “Please do remember to wrap it in a pcall.”

While filtering the string, it may fail to filter it. Pcalls are used to see if the function succeeded or not. If it does error, it returns the error message. Document about pcalls can be found here: Pcalls - When and how to use them

May you provide me with an example? That document doesn’t really explain alot, mostly talks about DataStorage

I can’t give you a whole script, but there is a devhub page about how to filter a string. Link is here: Text Filtering | Documentation - Roblox Creator Hub

I still don’t get it, is there an example that someone else has made that revolves around my systme as well?

The Developer Hub article shows an example of pcalling it.

You can either pass a function to the pcall or, use the shorthand way by passing it with . along with it’s self:

local TextService = game:GetService("TextService");
local UserId = 1;
local Text = "Hello, World!";
local Success, Response = pcall(TextService.FilterStringAsync, TextService, Text, UserId);

Refer to my tutorial on more about this shorthand.

1 Like

I’ve tried that fitting it into my code it didn’t work though.

Well can you please show how you’ve attempted to implement it?

local MessageToSet = table.concat(args, " ")

local filteredTextResult = TextService:FilterStringAsync(MessageToSet,1)

local TextService = game:GetService("TextService")

local filteredText = ""

local success, errorMessage = pcall(function()

filteredTextResult = TextService:FilterStringAsync(MessageToSet,1)

end)

if not success then

warn("Error filtering text:", MessageToSet, ":", errorMessage)

end

Error I get:

The sender must be connected to the current server
local MessageToSet = table.concat(args," ")
local filteredTextResult
local successFilter,errorFilter = pcall(function()
    filteredTextResult = TextService:FilterStringAsync(MessageToSet,player.UserId)
end)
if successFilter then
    local successConvert,errorConver = pcall(function()
        filteredTextResult = filteredTextResult:GetNonChatStringForBroadcast()
    end)
    if successConvert then
        print(filteredTextResult)
    end
end

What would I set the “player” to?

Like would it be:

local player = game:GetService("Players")

?

Sender is the player object, so you should do Sender.UserId.

It works now, but it doesn’t set the message.

CODE IN GUI:

-- [ STARTING VARIABLES ] --

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("Setmessage")

-- [ MAIN CODE ] --

Remote.OnClientEvent:Connect(function(filteredTextResult)
	script.Parent.MainFrame:TweenPosition(UDim2.new(0,0,0,0))
	
	script.Parent.MainFrame.msg.Text = (filteredTextResult)
	
	print("message set")
end)

Error? (30charrrrrrrrrrrrrrrr)

None. (30 chars, REEEEEEEEEEEEEEEEEE)

You either didn’t sent the filtered string in FireClient or the filtered string is still a Instance.