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

How can I make my script search for a word inside a sentence while still being case insensitive?

Asked by 4 years ago

My script can detect if a player said walter or walter7 in any case. I want to expand this to the whole sentence. For example if a player said ("Come Walter") it would still print their name.

for i,currentPlayersChatting in pairs(game:GetService("Players"):GetPlayers()) do
    currentPlayersChatting.Chatted:Connect(function(msg)
if string.lower(msg) == "walter" then
print(CurrentPlayersChatting.Name)
elseif string.lower(msg) == "walter7" then
print(CurrentPlayersChatting.Name)
end
    end)
        end

How would I expand this to search the whole sentence?

0
That means that you talked walter then it could be walter7..? Xapelize 2658 — 4y

1 answer

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

use string.find to check for a pattern in a string

also, if you're running the same code in the case of multiple different conditions, you should use or instead of a bunch of if/else statements

so, in your code, that would look like this

for i,currentPlayersChatting in pairs(game:GetService("Players"):GetPlayers()) do
    currentPlayersChatting.Chatted:Connect(function(msg)
        local lowerMsg = string.lower(msg)
        if string.find(lowerMsg, "walter") or string.find(lowerMsg, "walter7") then
            print(CurrentPlayersChatting.Name)
        end
    end)
end

also please indent your code properly in the future thaaaaaaaanks

Ad

Answer this question