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

How to check if certain word is used in chat?

game.Players.PlayedAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		print(msg)
		if msg == string.match(msg, "Hello") then 
			print(msg)
		end
	end)
end)

I have this script, so when a player chats something, and if that something is 'Hello', then it will print the message in the output. What I want though is sometimes players say 'Hello, how are you', or something like that, where they say 'Hello', but they add more to the message. Is it possible to pick it up if the word is used in a sentence.

Secondly, players often say 'hello' (lowercase H). Is their anyway to make it still work with lowercased letters?

Thanks

EDIT I added string.find, but if I say 'Hello abc' it doesn't work...

1 comment

0

if msg:lower():find("hello") then

1 answer

✓ Accepted answer

###Solution:

You can use the method in the string library match to find a capture given a particular pattern:

string.match(s, pattern [, i])

Where s is the string to find the match in, pattern is the string pattern, and i is an optional integer that determine at which index you start the search.

To ignore case, we can convert all the letters to a specific case (I choose lower, but upper would work too). We can do this with the method in the string library lower:

string.lower(s)

Where s is the string to convert.

###Example:

function saidWord(message, word)
	return string.match(string.lower(message), string.lower(word))
end

print(saidWord("Hello, my name is James", "hello"))
print(saidWord("goodbye", "hello"))

###Output:

>> hello
>> nil

Note that if the pattern is found, then saidWord will return the whatever word was in all lower case. You could get a Boolean value from this like so:

saidWord(msg, word) == string.lower(word)

5 comments

0

I editted my question. I'm trying to use string.find to see if the word was used in a sentence.. please help

0

You can do exactly what I did except replace match with find (even takes the same arguments). The only difference will be that it will return the start and end indices of the match instead of the match itself.

0

I replaced find with match, but it still doesn't work.... If I say Hello, it works, but if I say Hello blah, or if I add anything else to the sentence it won;t work. I tried adding words to Hello, like HelloA, and still doesn't work. I just want if some says 'Hello' in their chat it to print, not just if they say'Hello' and that's all

0

Post exactly what you are doing because it works flawlessly on my end

0

I editted it with what I have. It only works if I say 'Hello' and that's it

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