Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Why isn't my script checking for certain words in a string? [closed]

Asked by
Seyfert 90
8 years ago
Edited 8 years ago

This question already has an answer here:

How to check if string contains certain word?

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 
1
Your reason not to use string.find doesn't make any sense. string.find looks for characters and patterns in a string just like string.gmatch would. Same goes for string.match, string.gsub, etc. ScriptGuider 5640 — 8y
0
@ScriptGuider Okay, fine, but using string.find does not solve my issue as I have tried it.....do you have any ideas? Just pretend I changed it all to string.find as I will be doing, it still does not solve my issue sadly. Seyfert 90 — 8y
0
@ScriptGuider when I try doing string.find instead of string.gmatch nothing works...at all. I have tried string.gmatch(chosenMenuChoice, "one" and string.gmatch("one" , chosenMenuChoice) doesn't change the value, I even put a print under that line and still nothing. Seyfert 90 — 8y
0
Sorry meant string.find not string/gmatch Seyfert 90 — 8y
View all comments (5 more)
0
You're only using pattern matching functions to find out if a sequence of characters exists, so it doesn't matter which function you use, whether it's find, match, gmatch, etc. Your problem would be with something else. ScriptGuider 5640 — 8y
1
Please do not re-post the same question twice, unless if it's really necessary; you've already gotten answers on your past question, which explains what you can use & how you use them. TheeDeathCaster 2368 — 8y
0
@ScriptGuider I don't fully understand. Does this mean I can't do what I am trying to do with string.find ? The string.find command looks for patterns, yes? In the wiki I see it being able to find bla in blahblah , shouldn't it be able to find "one" in my string? Seyfert 90 — 8y
0
@TheAlphaStigma Similar question but my code is different and I have had 0 replies within 3 hours but as you can see I have gotten many replies here. The only "answer" I can think of is to use string.find which does not work for me, it does not even show any print statements. idk what to do. Seyfert 90 — 8y
1
Yes, if it exists. Like Pyrondon said, which I didn't think of, gmatch is going to return a function regardless of whether or not a pattern was found, and you're only checking if what it returns exists, which it will in every case. ScriptGuider 5640 — 8y

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?

1 answer

Log in to vote
4
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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.

0
@Pyrondon...ugh. As always you guys just go to critique instead of actually solving the problem. I tried changing string.gmatch to string.find and even if I put a print() on the linder under the first string.find the print does not show up. I have tried both string.find(chosenMenuChoice, "one") and string.find("one" , chosenMenuChoice) Seyfert 90 — 8y
0
@Seyfert Did you do the same changes for lines 8-15, or the following lines that also use gmatch? TheeDeathCaster 2368 — 8y
0
@TheAlphaStigma Yes sir I did, here it is http://pastebin.com/1Jc6sbnL Seyfert 90 — 8y
0
@Seygert You're using find incorrectly on line 10; switch the two argument around & it should work. :P TheeDeathCaster 2368 — 8y
0
@TheAlphaStigma, that is working but only for the ones for "one" and "two" the other ones don't work which mean it is my if/elseif statements that are making it weird? Gonna go to the drawing board. Seyfert 90 — 8y
Ad