How do I add a filter to a custom command?

  1. What do you want to achieve?
    A custom “/setmessage” command with filtering.
  2. What is the issue?
    I don’t know, it doesn’t display the Setmessage UI and the filter is an instance.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for solutions on the developer hub and forum, but it still didn’t solve my problem.

[ MAIN SCRIPT ] (Script in ServerScriptService)

-- [ 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," ")
	local filteredTextResult
	local successFilter,errorFilter = pcall(function()
    filteredTextResult = TextService:FilterStringAsync(MessageToSet,sender.UserId)
end)
	if successFilter then
    local successConvert,errorConver = pcall(function()
        filteredTextResult = filteredTextResult:GetNonChatStringForBroadcast()
    end)
    if successConvert then
        Remote:FireAllClients(filteredTextResult)
    end
	end
	
	print("CAC",filteredTextResult)
	
	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 --

[ UI SCRIPT ] (Script inside 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("RS",filteredTextResult)
	print("message set")
end)

[ MAIN LOCATION OF SCRIPTS, UI, ETC. ]
image

[ IMAGE OF OUTPUT WHEN RAN ]

It’s GetNonChatStringForBroadcastAsync() and not GetNonChatStringForBroadcast().

Thank you so much. It finally worked, I’ve been trying to fix it for 5 days.

1 Like