Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i detect if a player has inputted a certain character or symbol?

Today i was creating a discord feedback GUI but i didn't want people to be able to use commands through the feedback GUI for example "@everyone".

Would someone be able to show me how i would be able to detect a certain character or symbol. It would be helpful!

3 answers

✓ Accepted answer

I believe you're thinking if a player has a certain string in a textbox text. To do this, we can use string.match.

Here's an example of string.match:

local match = string.match("Welcome to my awesome game!", "game")
print(match) -- game

Another example:

str = "This is some text containing the word tiger."
if string.match(str, "tiger") then
  print ("The word tiger was found.")
else
  print ("The word tiger was not found.")
end

Okay, now let's do it with Textboxes.

First, let's make 2 variables called TextBox, which I'll make it be script.Parent, and one named the specific string.

local TextBox = script.Parent
local string = "Hello"

Once we do that, we'll have to detect if the player stops typing in the textbox, to do this we can use FocusLost.

The FocusLost event fires when the client lets their focus off the TextBox - typically when a client clicks/taps on a TextBox to begin text entry. This also fires if a TextBox forces focus on the user.

local TextBox = script.Parent
local string = "Hello"
TextBox.FocusLost:Connect(function()
print("Focus Lost")
end)

Now we do that, we'll have to detect if the string is inside of the textbox text, do to this, we will use string.match.

local TextBox = script.Parent
local string = "Hello"
TextBox.FocusLost:Connect(
    function()
        print("Focus Lost")
        if string.match(string, TextBox.Text) then
            print("The word Hello was found.")
        else
            print("The word Hello was not found.")
        end
    end
)

Not only you can use string.match, you can also use string.find

Example:

str = "This is some text containing the word tiger."
if string.find(str, "tiger") then
  print ("The word tiger was found.")
else
  print ("The word tiger was not found.")
end

String.find with TextBoxes:

local TextBox = script.Parent
local string = "Hello"
TextBox.FocusLost:Connect(
    function()
        print("Focus Lost")
        if string.find(string, TextBox.Text) then
            print("The word Hello was found.")
        else
            print("The word Hello was not found.")
        end
    end
)

Accept if it works.

1 comment

0

thank you! out of curiosity is there an opposite of "match" if so, what is it

Approaches

You can use the :gsub() method of String.

This will manipulate the string by replacing all occurrences the character queried, with the character provided. E.g:

local String = ("@everyone, let's all go to the park!"):gsub("@", "")
-- Optimal choice

By supplying a blank character, '', we essentially erase all occurrences of the symbol.


Alternatively, you may use :match() or :find():

• Match will return nil if it cannot locate an occurrence of the pattern given, otherwise, it will just give back the pattern.

local String = "@everyone, let's all go to the park!"

local SymbolInString = String:match("@")

print(SymbolInString) --> '@'

• Find will give then start and ending indices of the first occurrence.

local String = "@everyone, let's all go to the park!"

local WhereInString = String:find('@')

print(WhereInString) --> '1		1'

In any instance where either provide a valid result, you can convert it to a boolean or just pass the result through a conditional anyway:

local String = "@everyone, let's all go to the park!"

local FoundASymbol = (String:match("@") ~= nil) -- (String:find("@") ~= nil)

if (FoundASymbol) then
	print("Found!")
end

string:match() exists, it accepts regex, so basically

stringtocheck = "@everyone"
if stringtocheck:match(".*everyone.*") then -- I highly recommend using wildcards (this one does, you if do not want wildcards see the next one), there are ways to put characters in front and behind @everyone to allow bypassing non-wildcard fliters
	print("@everyone attempted")
end
if stringtocheck:match("@everyone") then -- if theres a @everyone word in the string
	print("@everyone attempted")
end

This discussion is preserved as it was. New answers, comments, and votes are closed.