How to get the message the player said in the chat

  1. What do you want to achieve?
    I want to get the message the player said in the chat.
  2. What is the issue?
    I don’t know how to use the Player.Chatted.
  3. What solutions have you tried so far?
    I searched for a while and found:
    Player.Chatted
    But I don’t know how to use it to get the message the player said in the chat…
2 Likes
Player.Chatted:Connect(function(msg)

print(msg) -- the msg parameter is the message send by the player
 
end)

Should be simple to learn.

1 Like

If you’re using Player.Chatted, you could check what he said, as @yyumchi gave you a short example of it, basically, that’s how the common ‘headless’ and ‘korblox’ commands work.

1 Like

It doesn’t work, I put this in Starter Character Script is that the cause?:thinking:

The usual way to use this script is by adding a script[server] and connecting to the player joined and then connect to the player messaging.

1 Like

Sorry :neutral_face: but I don’t understand what you said, I need to put this in a script in the ServeurScriptService ?

First things first, do you want to connect from a client script or a server script?

I just want to know what the player wrote in the chat :neutral_face:
So I don’t know which way to go…

1 Like

The best way to go is by the Player, so easily all you need to do is:

  1. Locate the player
game:GetService("Players").PlayerAdded:Connect(Player)  end)
  1. Connect to chat from the player
Player.Chatted:Connect(function(msg) end)
A full code sample just for you😉, do put it in a serverscriptservice
local plr = game:GetService("Players")

plr.PlayerAdded:Connect(function(Player)
	
	Player.Chatted:Connect(function(msg)
		
		print(msg) -- the player's message

	end)
	
end)
7 Likes

On the Studio Explorer on the left look for something Called ServerScriptService.
Then place a Server Script inside of it. Open it and paste this inside

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        -- Cloud_Emmie's code snippet
        print(msg) -- the msg parameter is the message send by the player
    end)
end
1 Like

This is how you do it

game.Players.PlayerAdded:Connect(function(plr)
   plr.Chatted:Connect(function(message)
    print(message)  -- prints message
  end)
end)
2 Likes

Thanks so much !!! :yum:
This helped me a lot ! :grin:
Have a good day !

I have one last small question, I need the message variable in a local script located in StarterCharacterScripts and I don’t know how I can transfer it…:sweat_smile:

Insert a Remote Event in Replicate Storage name it msg then change your code in sss:

local msgR = game.ReplicatedStorage.msg
local plr = game:GetService("Players")

plr.PlayerAdded:Connect(function(Player)
	
	Player.Chatted:Connect(function(msg)
		
	     msgR:FireClient(Player,msg) -- the player's message fired to client

	end)
	
end)

Client side this script:

game.ReplicatedStorage.msg.OnClientEvent:connect(function(msg)
print(msg) 
end)
1 Like

Sorry but i don’t really understand… :sweat_smile:
I want to get the message the player said in a local script in StarterCharacterScript :grinning:

If that’s not possible, can I get the message the player said in a local script located in StarterCharacterScripts? :thinking:

Well I showed you getting the message on client via RemoteEvents

1 Like

I need to create a remote event named “msg” and in my local script put this :

game.ReplicatedStorage.msg.OnClientEvent:connect(function(msg)
print(msg) 
end)

?

1 Like

You got it!
[Char Limit 30,Char]

1 Like

So that’s what I have do and it doesn’t work … :neutral_face:

1 Like