How to check if a player sent a message or used enter in chat

I want to detect if a player sent a msg in chat and gameProcessed only works when typing but i need to check when he pressed enter and sent the message
edit: i want it in UserInputService

Use this to detect messages:

game.Players.PlayerAdded:Connect(function(player)
   player.Chatted:Connect(function(message)
      print("player "..player.Name.." sent message: "..message)
   end)
end)

i hope it helped you

2 Likes
  • Firstly you’ll need to get the player, you can use a .PlayerAdded event for that.
  • Secondly, you need to listen for when a player sends messages using the PlayerInstance.Chatted event.
  • Thirdly, I recommend checking if there are any existing players in-game before the .PlayerAdded runs, as the players might have joined before the code began to run.

That being said, here’s an example of how you can accomplish that:

local Players = game:GetService('Players')


local function PlayerAdded(Player)
	local function Chatted(Message)
		print('Player %s has sent a message: %s'):format(Player, Message)
	end
	Player.Chatted:Connect(Chatted)
end


local GetPlayers = Players:GetPlayers()
for i = 1, #GetPlayers do
	local Player = GetPlayers[i]
	coroutine.resume(coroutine.create(function()
		PlayerAdded(Player) -- Runs this function in parrallel for each player added before .PlayerAdded was fired.
	end))
end
Players.PlayerAdded:Connect(PlayerAdded)

Now, for the UserInputService: This is a client-sided service, you’ll need to use RemoteEvent:FireClient from the server script (assuming you want something done when the player chats, fire the RemoteEvent once the player chats via the server script).

RemoteEvent:FireClient(Player, Message) -- The first parameter on the OnClientEvent event will be the message.
2 Likes