String.find() finding keywords in words

Hello, I am working on a Chat Bot using string.find(). However string.find() finds keywords in other words?
For example:
“what are you doing?”
returns
“hello”
Because I have added this in my code:

string.find(msg, "whats up")

Sorry if this doesnt make sense, im bad at explaining lol.
Anyways I’ve tried putting certain triggers above others but it doesn’t do much because then there are other bugs.

So is there a way to search for searching full words in strings and not look for them in other words?
Again sorry if this is very confusing. Hopefully you get my point.

you can make it

string.find(string.lower(msg), "whats up"-- keep it all small caps)

so if the user chat any caps like “Whats up” it’ll still run.

Can i ask? can u share the script of

function ()
local function (msg)
-- your string.find script
end
end

?

I have a variable using string.lower() for the message

local msg = string.lower(m)

My issue is, if I say something like “How are you?” it says:
Hello
This is because the word “you” has “yo” in it (Which is a word which triggers the bot to say hello.)
What I want to achieve is for it to only read the full word.

There are a couple of ways this is usually achieved.

One common way is to split the string into an array of words (usually called tokenising) then search the array for matches.

Another is using what is called a Regular Expression or RegEx, which lets you define a pattern for matching within a string then testing the string with the expression. Sadly Luau doesn’t support RegEx but it does support pattern matching with string:match or string.find, which are similar to RegEx but less powerful and uses a different syntax.

You can find more here, but I think something like this is what you need

local currentTrigger = "yo"
local result = string.find(msg, "%s"..currentTrigger.."%s")

Alternatively you might prefer the match instance method of the message string

local currentTrigger = "yo"
local result = msg:match("%s"..currentTrigger.."%s")

I imagine you would be using a loop to iterate through a set of trigger words.

4 Likes

You can disable pattern matching for string.find/string:find if the 3rd/4th argument respectively is a Boolean value of true, this 3rd/4th optional parameter represents whether or not to perform a plain string search and by default is set to false.

Just a little additional info to what you already provided.

2 Likes