Detecting Commands by Message

Learn how to detect commands when the Player chats!

by 41444ds55

Author Avatar

##Chatted Function

As everyone knows, Chat is a function of Roblox that is used to communicate with others.

Chat is generally used to talk with players, but it can be also a way to execute commands by messages!

Let's make an example.

I want the game to print "Hi" everytime I send a message with the string "Hello World!"

This is an example of how to detect when the player sends a message in the Chat.

game.Players.PlayerAdded:Connect(function(Player)
       Player.Chatted:Connect(function(Message)
                 --[ When the player sends in a message, it returns as a string in scripts.
                 --[ With the message string you can do anything you want, for example detecting words, letters, etc.
       end)
end)

##Making Commands

You see, we've already made the Message function, now, after it returns the message string, we are gonna detect if the Player is chatting "Hello World!"

game.Players.PlayerAdded:Connect(function(Player)
       Player.Chatted:Connect(function(Message)
              if Message == "Hello World!" then --[ If chatted Hello World..
                      print("Hi!") --[ Return a Hi!
              end
       end)
end)

And thats how you detect a message when the player chats!

Now what about we create a command that inserts a new Part in the workspace? We will need to detect if the player chats "Add Part", if the string is "Add Part", then its gonna create a new Part parented to the workspace!

game.Players.PlayerAdded:Connect(function(Player)
       Player.Chatted:Connect(function(Message)
              if Message == "Add Part" then
                      local Part = Instance.new('Part', workspace)
                      Part.Name = 'CommandPart'
              end
       end)
end)

And thats one of the Chat's functionabilitys!

View in-game to comment, award, and more!