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)
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
thank you! out of curiosity is there an opposite of "match" if so, what is it