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
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:
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
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
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
"GMatch" Function - Tbh, this was a nightmare ;-;
The Output - Retrieves & shows data (such as strings & numbers), warnings, & errors.
Tables -- Hold (or contains) data, such as strings, numbers, objects, etc.
I hope this helped you in any way. :)
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
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
Use string,find()
Here is an example here:
local myString = "Hi there, Welcome to Roblox!" --makes a var with a string val if string.find(myString, "Roblox") then print("Roblox was found in the myString variable.") end --[[ what the if statement does: if it detects roblox within the string value with string.find() then it will print: Roblox was found in the myString variable. This statement however will always be true, since Roblox is a part of the string value in myString. ]]--
You can edit this upon what you like, this shouldn't be your full script.