Find And Replace String "TeST"?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create a script that will replace a given string that is not allowed to another string through LocalScript
    Example:
    *Server does not allow the word “cheese”, the replacement word “banana” *

And I’m typing ‘imcheese’ and it will turn into 'imbanana’
*Typing
image
Replaced
image

  1. What is the issue? Include screenshots / videos if possible!
    It only works great when the string the player is entering is not uppercase, otherwise it will spam a lot of replacement strings and
    it will not remove capital letters
    image
    image

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes, but those are posts that need help how to using roblox studio features

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

function AutoDetect(UI:TextBox)
	UI.Changed:connect(function(P)
		if tostring(P) == "Text" then
			for i,Name in pairs(DontThisTexts) do
				if string.match(string.lower(UI.Text),string.lower(Name)) then
					if UI:IsA("TextBox") then
						--The Main Is Here,What I Wrong With This Script???
						local V = UI.Text:split(Name)
						UI.Text = ''
						
						if V[2] ~= nil then
							UI.Text = V[1]..Replacer..V[2]
						elseif V[1] ~= nil then
							UI.Text = V[1]..Replacer
						elseif V[1] == nil then
							UI.Text = Replacer
						end
					end
				end 
			end
		end
	end)
end

for _,UI in pairs(game.Players.LocalPlayer:WaitForChild("PlayerGui"):GetDescendants()) do
	AutoDetect(UI)
end
game.Players.LocalPlayer:WaitForChild("PlayerGui").DescendantAdded:connect(function(UI)
	AutoDetect(UI)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

[Edited Script] not best but it’s good (:

function AutoDetect(UI:TextBox)
	UI.Changed:connect(function(P)
		if tostring(P) == "Text" then
			for i,Name in pairs(DontThisTexts) do
				--(if string.match(string.lower(UI.Text),string.lower(Name)) then) I Removed This
					if UI:IsA("TextBox") then
						local function createPattern(word: string): string
							local sets = {}
							
							for letter in string.gmatch(word, ".") do
								table.insert(sets, ("[%s%s]+"):format(string.lower(letter), string.upper(letter)))
							end
							return table.concat(sets, "")
						end
						local ThisNew,_ = string.gsub(UI.Text,createPattern(Name),Replacer)
						UI.Text = ThisNew
					end
				--end
			end
		end
	end)
end

for _,UI in pairs(game.Players.LocalPlayer:WaitForChild("PlayerGui"):GetDescendants()) do
	AutoDetect(UI)
end
game.Players.LocalPlayer:WaitForChild("PlayerGui").DescendantAdded:connect(function(UI)
	AutoDetect(UI)
end)
1 Like

You can use string.gsub() for this.

local str: string = "imcheese"
local sub: string, _ = string.gsub(str, "cheese", "banana")

warn(sub)
3 Likes

Oh,it’s so good but it doesn’t change capital letters, is we can detect ‘Cheese’ without string.lower and string.upper?
Because I want it from “I Am Cheese!” will return “I Am banana” without changing the other letters, is it possible?

the second argument takes a type of regular expression, we can also check for capital letters like so

local sub: string, _ = string.gsub(str, "[cC][hH][eE][eE][sS][eE]", "banana")

This will detect any variation of capital letters without lower casing the whole string. You could even use the + pattern item to match multiple e so “Cheeeeeeeeeeeeeeeeeeeese” gets replaced too

local sub: string, _ = string.gsub(str, "[cC][hH][eE][eE]+[sS][eE]", "banana")
2 Likes

It’s works!,hmmm so how we can convert the normal string ‘Cheese’ into ‘[cC][hH][eE][eE]+[sS][eE]’ with script?

converts example “Cheese” → “[cC][hH][eE][eE][sS][eE]”
it does not add a + sign lets say example [cC][hH][eE][eE]+[sS][eE]

function createPattern(word: string): string
    local sets = {}

    for letter in string.gmatch(word, ".") do
        table.insert(sets, ("[%s%s]"):format(string.lower(letter), string.upper(letter)))
    end

    return table.concat(sets, "")
end

usage:

print(createPattern("Cheese"))
1 Like

Wow that’s amazing!, and so are you,@S3nt1ne3l @gertkeno @Downrest thank you all :3

2 Likes