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

How would I make this check for a word within a sentance?

Asked by 10 years ago
game.Players.jaytheidiot.Chatted:connect(function(Msg)
if Msg == "Cheese" then
Song:Stop()
Song.SoundId = "rbxassetid://145143628"
Song:Play()
end
end)

How would I make this check for the word "cheese" in a sentence. Not case sensitive?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

To this case insensitive, we just operate on a lowercase version of the text:

msg = msg:lower()

We can use Lua's pattern matching to find something which a content is punctuation or space separated, if you actually require it to be a word (e.g., will not accept "cheesewheel" but will accept "cheese wheel")

msg = " " .. msg .. " ";

local cheeseFound = 
    msg:find("[%s%p]+cheese[%s%p]+");

Alternatively, if you just need to find "cheese" somewhere in the text, but not necessarily as its own word:

local cheeseFound = msg:find("cheese");


Post Script:

If an answer explains all you need, mark the answer as accepted so that Helpers and Askers have understand the solution

Ad

Answer this question