How to check if a TextBox contains a specific string?

Title says it all.
Example:
If the string is “hi” then it will work
If the string is “hi hi” then it will work
If the string is “hello” then it will not work


2 Likes

if Textbox.Text == "..." then
end

Just use
if script.Parent.Text == “???” then
–assuming this is correct, the location and text
end

Use string.sub() to see if you can find the text in the TextBox.

if Textbox.Text == "yourString" then
end
if string.find(TextBox.Text, yourstring) then
    print("Found pattern")
end

The best would probably be this:

local TextBox = script.Parent --// Change this variable to the actual TextBox

ExampleButton.MouseButton1Click:Connect(function()
    local ex = string.lower(tostring(TextBox.Text))
    if ex == "hi" or ex:find("hi") then
        --// What happens when it gets authorized? e.g print("User entered:'hi'!")
    end
end

You can change ‘ExampleButton’ if you want it to be entered when it’s clicked by a button, but if not then you’d do this:

local TextBox = script.Parent --// Change this variable to the actual TextBox

TextBox.InputChanged:Connect(function() --// Input changed is when a letter in the text is changed by the LocalPlayer
    local ex = string.lower(tostring(TextBox.Text))
    if ex == "hi" or ex:find("hi") then
        --// What happens when it gets authorized? e.g print("User entered:'hi'!")
    end
end
8 Likes

This wouldn’t work as he didn’t want it to be a find, he wanted it if it was just the message, this is what he wrote:

Title says it all.
Example:
If the string is “hi” then it will work
If the string is “hi hi” then it will work
If the string is “hello” then it will not work

I’m not trying to be rude, as I would do the same as your code, but this is what he requested, and I’m just telling you this.

The way you did it only works if the text is “hi”, it wouldn’t work if it was “hi hi”. He said if the TextBox contains a specific string and has shown examples of what he wanted, my code does what he wanted, if it finds a match, then do the if statement code, else do nothing if it finds no match

Oh, I see the problem and I just read it wrong lol. Sorry about that

It’s fine, sometimes we can mess up in how we read something, at least you understood what was meant by @iShouldG0 and fixed your solution, although I would recommend just making the if statements as

if ex:find("hi") then

because it’s almost definitely going to check using the find method anyways so the first check for equality is a bit unneeded imo

idk why but I almost always find you in any post I go to lol. And I agree, the first check isn’t needed. Sometimes I do that just to make sure it checks it.

Haha, I’m just very active in Scripting Support as I like to help others with their problems whenever I can!

I am the same, I just like helping people.
By the way, your silence recreation script system demo is really good!
(That was a very long name though lol)

Thanks! The game is quite old as I haven’t worked on it in a long time due ot burn out. Also I think it’s best we move this conversation else where, perhaps in Messages if needed

no need to tostring TextBox.Text as it is already a string