Player.Chatted event won't output chat message

Hello once again, DevForum!

I’ve run into an issue where, while trying to make a chat bot, the bot won’t chat back when I type a message. To be clear, I’m making a filter with tables to see if the message includes one of the filters and will return a chat message from the bot like “Well, thank you! [Insert player name] .” if the player compliments the bot. Problem is that it wont chat back no matter what I do. Could really use some help on this script:

local function isGreeted()
	chat:Chat(script.Parent.Parent.Head, "Greetings,"..player.Name, Enum.ChatColor.Blue)
	print("M.A.P has succesfully greeted with "..player.Name..".")
	greeted = true
	interacted = true
end

local function isComplimented()
	chat:Chat(script.Parent.Parent.Head, "Well, Thank you, "..player.Name, Enum.ChatColor.Green)
	print("M.A.P has succesfully greeted with, "..player.Name..".")
	greeted = true
	interacted = true
end

local function readMessage(msg) --Im so smart
	if msg then
		local msgsplitted = string.split(msg, ",")
		local spaced = string.split(msg, " ")

		local numberedWords = #spaced
		local msgsplittedNumbered = #msgsplitted
		
		if msg == true then
			for i = 1, #greetings do --first word
				local msgcorrect = string.lower(msgsplitted[1])

				if msgcorrect == greetings[i] then
					isGreeted()
					print("greeted by"..player.Name)

				end
			end
			for i = 1, #compliments do --first word
				local msgcorrect = string.lower(msgsplitted[1])
				
				if msgcorrect == compliments[i] then
					isComplimented()
					print("complimented by "..player.Name)
				end
			end
		end

	else
		print("huh how")	
	end
end

player.Chatted:Connect(readMessage)

Maybe I should add

game.Players.PlayerAdded:Connect(function()
     player.Chatted:Connect(readMessage)
end)

at the end of the script? I don’t know.

Sidenote : All this is in a local script

How has player been defined? Considering it’s integral to the script’s functionality.

local player = game.Players.LocalPlayer

Before I would use service but this is a LocalScript and LocalPlayer would be a better option.

(Sorry for late reply)

local function isGreeted()
	chat:Chat(script.Parent.Parent.Head, "Greetings,"..player.Name, Enum.ChatColor.Blue)
	print("M.A.P has succesfully greeted with "..player.Name..".")
	greeted = true
	interacted = true
end

local function isComplimented()
	chat:Chat(script.Parent.Parent.Head, "Well, Thank you, "..player.Name, Enum.ChatColor.Green)
	print("M.A.P has succesfully greeted with, "..player.Name..".")
	greeted = true
	interacted = true
end

local function readMessage(msg) --Im so smart
	if msg then
		local msgsplitted = string.split(msg, ",")
		local spaced = string.split(msg, " ")

		local numberedWords = #spaced
		local msgsplittedNumbered = #msgsplitted
		
			for i = 1, #greetings do --first word
				local msgcorrect = string.lower(msgsplitted[1])

				if msgcorrect == greetings[i] then
					isGreeted()
					print("greeted by"..player.Name)

				end
			end
			for i = 1, #compliments do --first word
				local msgcorrect = string.lower(msgsplitted[1])
				
				if msgcorrect == compliments[i] then
					isComplimented()
					print("complimented by "..player.Name)
				
			end
		end

	else
		print("huh how")	
	end
end

player.Chatted:Connect(readMessage)

try this

i remove the line if msg == true

Nothing happened. No errors, no output printing, nothing ):

local function isGreeted()
	chat:Chat(script.Parent.Parent.Head, "Greetings,"..player.Name, Enum.ChatColor.Blue)
	print("M.A.P has succesfully greeted with "..player.Name..".")
	greeted = true
	interacted = true
end

local function isComplimented()
	chat:Chat(script.Parent.Parent.Head, "Well, Thank you, "..player.Name, Enum.ChatColor.Green)
	print("M.A.P has succesfully greeted with, "..player.Name..".")
	greeted = true
	interacted = true
end

local function readMessage(msg) --Im so smart
		local msgsplitted = string.split(msg, ",")
		local spaced = string.split(msg, " ")

		local numberedWords = #spaced
		local msgsplittedNumbered = #msgsplitted
		
			for i = 1, #greetings do --first word
				local msgcorrect = string.lower(msgsplitted[1])

				if msgcorrect == greetings[i] then
					isGreeted()
					print("greeted by"..player.Name)

				end
			end
			for i = 1, #compliments do --first word
				local msgcorrect = string.lower(msgsplitted[1])
				
				if msgcorrect == compliments[i] then
					isComplimented()
					print("complimented by "..player.Name)
				end
	end
end

player.Chatted:Connect(readMessage)

uh what about this

Actually I accidentally put it in workspace cause it said something about how head is not a part of the script’s parent. Sorry.

It works after I put it in replicated storage, I thought the solution was to add a localscript in replicated storage and another script in server script service and use a remote function but this is so much simpler, thanks a lot!

niceeeeeeeee