Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

0

That means that you talked walter then it could be walter7..?

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.