What do you want to achieve?
A “/setmessage” command that has filtering.
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?”
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 --
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)
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
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.
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