Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to check if string contains certain word?

Asked by
Seyfert 90
3 years ago
Edited 3 years ago

I am trying to make ice cream choices, if the customer's dialog contains certains keyword it is supposed to change the name of an IntValue, problem is my if statements seem to be messing up and it is choosing the wrong value.

My script first checks how many scoops they have, one or two, then it goes by flavors. If they have two scoops it first checks if they spoke of two flavors, then only to one flavor.

local chosenMenuChoice = {"One scoop of vanilla please", "I want two scoops of vanilla and chocolate"}
local iceCreamChoice = script.Parent.icecreamchoice

if string.gmatch("one", chosenMenuChoice) then
            if string.gmatch("vanilla", chosenMenuChoice) then
                iceCreamChoice.Value = "osv"
            elseif  string.gmatch("chocolate", chosenMenuChoice) then
                iceCreamChoice.Value = "osc"
            elseif  string.gmatch("mint", chosenMenuChoice) then
                iceCreamChoice.Value = "osm"            
            end
        end
        if string.gmatch("two", chosenMenuChoice) then
            if string.gmatch("vanilla and chocolate", chosenMenuChoice) then
                iceCreamChoice.Value = "tsvc"           
            elseif string.gmatch("vanilla and mint", chosenMenuChoice) then
                iceCreamChoice.Value = "tsvm"           
            elseif string.gmatch("chocolate and mint", chosenMenuChoice) then
                iceCreamChoice.Value = "tscm"           
            end
            if string.gmatch("vanilla", chosenMenuChoice) then
                iceCreamChoice.Value = "tsv"
            elseif  string.gmatch("chocolate", chosenMenuChoice) then
                iceCreamChoice.Value = "tsc"
            elseif  string.gmatch("mint", chosenMenuChoice) then
                iceCreamChoice.Value = "tsm"            
            end
        end
0
string.gmatch, string.find etc. RubenKan 3593 — 3y
0
Right here is everything with string manipulation -->> http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation rustyhuskey 59 — 3y
0
Looking at it, I think you've just muddled the arguments up. The first argument should be the string your checking, not the phrase that you are trying to find in the string JohnnyMorganz 185 — 3y
0
@JohnnyMorganz I have tried now with if string.find("one", chosenMenuChoice) then but anything below that does not work for some odd reason, print("test") doesn't print either. Seyfert 90 — 3y

3 answers

Log in to vote
5
Answered by 3 years ago
Edited 3 years ago

Before I begin, I would like to point out that you're not using GMatch correctly, based upon from the research I conducted, as I found that it's used for Loops, and not itself alone (According to the Wiki, at least). I would also like to disclaim that I will not be rewriting your code, otherwise I'm doing the work for you. :P

Now, to begin. >:)

To accomplish what you're trying to do, there're 2 ways you could go about this; you could use:

  1. GMatch (when used correctly) - Uses String Patterns (as far as I know); however, you would not use this to accomplish what you're trying to do

  2. Find - This will only return the position of where the keyword is at, and not the word itself; it will also only search for the matching keyword, then stop the iteration

  3. Match - Similar to Find, but the difference is that it'll return the word (not the entire word, however)

Before we proceed, I would like to add a string & a table to handle all this, w/o the need of re-typing over-and-over again:

local String = 'Hello World! I\'m on Scripting Helpers!' -- This string will be used in the code to example & show what the listed functions (& loop/ function thingy..?) do; variable "String" represents the string it is set to

String = string.lower(String) -- Lower-cases the entire string; string.lower is used to lowercase a specified string; I am aware of String:lower(), but I wanted to take a step back for once :P

local toFind = { -- Table "toFind" will handle the data (personally, I like to keep my code neat & clean in this fashion :) ); "toFind" contains the tables "Strings" & "StringPatterns," which contain the data to use later on (although, both serve difference functions)
    -- Tables are used to hold data & information, such as objects, strings, numbers, etc.
    Strings = { -- Table "Strings" will be used for functions "Find" & "Match,"; table "Strings" contains 2 of the keywords that the later functions will use
        'world', -- String 'world' is a, well, string value, and will be used for the later functions
        'hello' -- Same function will be performed as w/ string 'world'
    }, -- Ends data (storage) (for table "Strings")
    StringPatterns = { -- I'm not familiar w/ String Patterns very well, but I'll do my best to explain; String Patterns are used to get the parts of a string & return them, or get a match(es) in strings(?) (I'm not too sure how to explain this; if anyone has any information or a better explanation, please inform me & let me know ;-;)
        'h%a+' -- This will be used for "GMatch"; the "h" in the string is used to return keywords that start w/ a, well, "h", while "%a+" will be used to return the whole word; "%a" in String Patterns is used to match lower & upper case characters (letters), while the "+" is used to retrieve the full word(s)/ string (the Wiki explains this better ;-;)
    }
}

Lets start w/ GMatch: GMatch is a function that will iterate through the string, then return them( I believe? If anyone has a better explanation to this, please let me know; I'm not familiar w/ GMatch, and I'm going off of the Wiki & how its used); think of it as a table, but not a table at the same time:

for v in string.gmatch(String, toFind.StringPatterns[1]) do -- Variable "v" represents the word/ letter/ etc returned from "GMatch," while "GMatch" returns the characters (letters, numbers, etc) found w/in the specific string ("String" in this case)
    print(v, 'GMatch Result') -- Will print the word "v" represents, & will print "GMatch Result" on the same line; print, well, prints data into the Output
end

Now, after the code is fired, it'll return 2 results in the Output; this is what will be returned:

--[[
    hello GMatch Result -- B/c we gave "GMatch" an argument to return words that start w/ an "h" & return the whole word, it prints this & the following (I hope this better explains what I talked about earlier ;-;)
    helpers GMatch Result
--]]

However, this will not help you accomplish what you're trying to do.

Now, onto functions I'm more familiar w/! :D We'll be using function Find in our next example, following function "Match". :)

For the "Find" function, it's used to iterate through a specified string (value), and return the first keyword found; however, it will not return the word itself, but the position of it. This is how the "Find" function would be used:

print(string.find(String, toFind.Strings[1]), 'Find Result') -- I don't think I need to explain "print" again :P
-- "Find" has 2 arguments that you need to give to it: the first argument is the string to iterate through (durr <->), and the second argument is the word to find when iterating & return (its position)

And when we fire that above code, this is what will be returned in the Output:

--[[
    7 Find Result -- Note how it returned "World"s position, which is 7 characters after "Hello" begins, and not the word itself
--]]

But, that's only one of the 3 that can be used! :O

Now, for the "Match" function, it's used to iterate through a specified string (value), and return the first keyword found; unlike the "Find" function, it returns the word (it was given) that was found. This is how the "Match" function would be used:

print(string.match(String, toFind.Strings[2]), 'Match Result') -- Again, I don't think I need to explain "print" again XP
-- "Match" also has 2 argument that you need to give it: the first being the specified string to iterate through, and the second being what keyword to find & return it

Aaaaand when we fire the above code, this is what will return in the Output:

--[[
    he Match Result -- Note how it returned the keyword, unlike how "Find" only returned the position; although, "Match" only returned the keyword, 'he,' which was given to it, and not the entire word; I believe that's where "GMatch" shines when it comes to this
--]]

And wa-la! That's 3 ways to check & find specific a keyword(s) in a string! :D Plays Final Fantasy Victory Theme

I forgot to mention before that w/ "Find" & "Match", if they don't find the keyword that was given to them, they will return nil, so be careful when using them. :o

Stuff touched on, but didn't go into great detail about

  1. "Find" Function

  2. "Match" Function

  3. "GMatch" Function - Tbh, this was a nightmare ;-;

  4. String Patterns

  5. The Output - Retrieves & shows data (such as strings & numbers), warnings, & errors.

  6. Loops

  7. Tables -- Hold (or contains) data, such as strings, numbers, objects, etc.

  8. Functions

  9. Nil

  10. Iteration

I hope this helped you in any way. :)

0
Holy jesus. wilsonsilva007 354 — 2y
0
u good bro @thee greatneil80 2309 — 1y
0
^ wat TheeDeathCaster 2319 — 1y
0
too many comments NERD greatneil80 2309 — 1y
0
no sense iiClearlyDeveloper 29 — 11mo
Ad
Log in to vote
0
Answered by
DevKurka 173
3 years ago
Edited 3 years ago

Use string.find(string, word) function Example:

local message = "Hello there"
if string.find(message, "Hello") then
 print("Found Hello")
end
if string.find(message, "Hai") then
 print("Found Hi")
end
-- output will be Found Hello cause there is only Hello in the message string (There is no Hai word)

There is fixed script

local chosenMenuChoice = {"One scoop of vanilla please", "I want two scoops of vanilla and chocolate"}
local iceCreamChoice = script.Parent.icecreamchoice

if string.find(chosenMenuChoice, "one") then
    if string.find(chosenMenuChoice, "vanilla") then
                iceCreamChoice.Value = "osv"
            elseif  string.find(chosenMenuChoice, "chocolate") then
                iceCreamChoice.Value = "osc"
            elseif  string.find(chosenMenuChoice , "mint") then
                iceCreamChoice.Value = "osm"            
            end
        end
        if string.find(chosenMenuChoice, "two") then

            if string.find(chosenMenuChoice, "vanilla") then
                iceCreamChoice.Value = "tsv"
            elseif  string.find(chosenMenuChoice, "chocolate") then
                iceCreamChoice.Value = "tsc"
            elseif  string.find(chosenMenuChoice, "mint") then
                iceCreamChoice.Value = "tsm"            
            end
    if string.find(chosenMenuChoice, "vanilla and chocolate") then
                iceCreamChoice.Value = "tsvc"           
            elseif string.find(chosenMenuChoice, "vanilla and mint") then
                iceCreamChoice.Value = "tsvm"           
            elseif string.find(chosenMenuChoice, "chocolate and mint") then
                iceCreamChoice.Value = "tscm"           
            end
        end

0
Just use in script find instead of gmatch, cause gmatch returns table, but find finds. DevKurka 173 — 3y
0
gmatch always return value so its not nil and first thing in each if condition is "called" because thing which gmatch return isnt nil. its just empty array. DevKurka 173 — 3y
0
@kurka123 I have changed it to string.find but nothing prints or my StringValue does not change, I am guessing my if statements are the cause? Any tips on how to clean it up? Seyfert 90 — 3y
0
For example, "if string.find("one", chosenMenuChoice) then" does not seem to be doing my print("test") Seyfert 90 — 3y
View all comments (3 more)
0
flip it arg1 (in your example "one") might be string WHERE YOU ARE TRYING TO FIND THING, arg2 should describe string WHICH YOU ARE TRYING TO FIND. For example string.find("Hey guys", "Hey") will be true cause there is "Hey" in the "Hey guys". Just flip arguments (if string.find(chosenMenuChoice, "one") then) DevKurka 173 — 3y
0
there is working script. Also sorry for inactivity but I was on phone. DevKurka 173 — 3y
0
I editted answer DevKurka 173 — 3y
Log in to vote
0
Answered by 3 years ago
str = "Hello World" -- The String
print(string.find(str, "World")) -- Prints the start char number of the string, and the end char number of the string
0
I don't want to print anything though....I want a StringValue to be changed. This doesn't really help me because it is confusing. Seyfert 90 — 3y

Answer this question