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 5 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.

1for i,currentPlayersChatting in pairs(game:GetService("Players"):GetPlayers()) do
2    currentPlayersChatting.Chatted:Connect(function(msg)
3if string.lower(msg) == "walter" then
4print(CurrentPlayersChatting.Name)
5elseif string.lower(msg) == "walter7" then
6print(CurrentPlayersChatting.Name)
7end
8    end)
9        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 — 5y

1 answer

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
5 years ago
Edited 5 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

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

also please indent your code properly in the future thaaaaaaaanks

Ad

Answer this question