Okay first, don't tell me to go use string.find because that is not what I am to use, string.find only finds patterns/characters, not words. string.gmatch should work just fine.
Now, I am making script for ice cream. First it checks if the string specifies one scoop, if it specifies two scoops then it goes to two scoops in there. From then on for each one and two scoops it tries to check if the string had the flavor vanilla, or chocolate, or mint. However, even if the string contains chocolate and no vanilla it is still changing the value to the value if it found vanilla.
local menuChoices = {"one scoop of vanilla please", "I want one one scoop of chocolate", "gimmie two scoops of vanilla" } local IceCreamChoice = script.Parent.StringValue local chosenMenuChoice = menuChoices[math.random(1, #menuChoices)] brick.Name = chosenMenuChoice if string.gmatch(chosenMenuChoice, "one" ) then if string.gmatch(chosenMenuChoice, "vanilla") then iceCreamChoice.Value = "osv" elseif string.gmatch(chosenMenuChoice, "chocolate") then iceCreamChoice.Value = "osc" elseif string.gmatch(chosenMenuChoice, "mint") then iceCreamChoice.Value = "osm" end elseif string.gmatch("two", chosenMenuChoice) then if string.gmatch(chosenMenuChoice, "vanilla") then iceCreamChoice.Value = "tsv" elseif string.gmatch(chosenMenuChoice, "chocolate") then iceCreamChoice.Value = "tsc" elseif string.gmatch(chosenMenuChoice, "mint") then iceCreamChoice.Value = "tsm" end end
Yeah, uh, I know the first thing you said was don't tell me to go use string.find, but..
that's sort of what you need to use. Your declaration that "string.find only finds patterns/characters, not words. string.gmatch should work just fine" is false; for one, string.find
can be used to find words just as it can be used to find patterns:
print(('Hello world'):find('Hello'));
Would result in 1 and 5; the start of the match and the end of it.
Secondly, string.gmatch
doesn't do exactly what you may think it does. As the wiki states, it "Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call"1.
So, even if there are no matches, it will still return a function. string.gmatch
is not what you need for this, and changing it to string.find
(or string.match
) would solve your problem.
Hope this helped.
Marked as Duplicate by TheeDeathCaster, jjwood1600, and RubenKan
This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.
Why was this question closed?