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.
1 | for i,currentPlayersChatting in pairs (game:GetService( "Players" ):GetPlayers()) do |
2 | currentPlayersChatting.Chatted:Connect( function (msg) |
3 | if string.lower(msg) = = "walter" then |
4 | print (CurrentPlayersChatting.Name) |
5 | elseif string.lower(msg) = = "walter7" then |
6 | print (CurrentPlayersChatting.Name) |
7 | end |
8 | end ) |
9 | end |
How would I expand this to search the whole sentence?
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
1 | for 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 ) |
8 | end |
also please indent your code properly in the future thaaaaaaaanks