Filter text automatically

This is a script that I use to receive the text that players put in a textbox. It works fine but isn’t there a way to filter the words automatically and not manually like I’m doing?

local RS = game:GetService('ReplicatedStorage')
local ChangeNameEvent = RS:WaitForChild("remoteEvents"):WaitForChild("ChangeName")
local UpdateNameEvent = RS.remoteEvents.UpdateName

local bannedWords = {"c", "b", "a"}


ChangeNameEvent.OnServerEvent:Connect(function(player, petName, petID)

	-- Check if the pet name contains any banned words
	for _, word in ipairs(bannedWords) do
		if string.find(petName:lower(), word) then
			-- If the pet name contains a banned word, replace it with asterisks
			petName = string.gsub(petName, word, string.rep("*", #word))
		end
	end

	
	for _, v in ipairs(player:WaitForChild("Pets"):GetChildren()) do
		local ID = v:WaitForChild('PetID')
		if ID.Value == petID then
			local Named = v:WaitForChild('Named')
			Named.Value = petName
			UpdateNameEvent:FireClient(player, petName)
		end
	end
end)

Yes, theres a way:

local Text = ""
local success, er = pcall(function()
	local Result = TextService:FilterStringAsync(NameText, Player.UserId)
		Text = Result:GetNonChatStringForBroadcastAsync()
	end)

	if not success then
		print("Error when filtering text: " .. er)
	return
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.