How can I detect what a player says?

I’m trying to detect what a player says and if they say something specific then it runs that piece of code. I was using this but it runs every time you chat not just when you say “Breath Air”

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message == "Breath Air" or "BREATH AIR" or "breath air" then 

All help would be appreciated

Your understanding of logical operators seems to be a bit flawed.

This expression:

message == "Breath Air" or "BREATH AIR" or "breath air"

is equal to this:

(message == "Breath Air") or ("BREATH AIR") or ("breath air")

In this case, this would evaluate to "BREATH AIR" if the message doesn’t equal to "Breath Air" and this would pass though the conditional because a string is not false or nil.

A better way to do is that you would lowercase all of the characters in the message and check for every possible combination with a single check:

if string.lower(message) == "breath air" then
2 Likes

thanks alot. I used your second method which is perfect but I am curious why would you add the brackets in my old method?

That was for visualisation to show why it doesn’t work. Your old method would only check for "Breath Air" while giving the other two a pass, which in return, would always evaluate to a truthy value.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.