Chat commands

Learn how to make chat commands.

by caiomgt

Author Avatar


Before you start, this tutorial assumes you know how events work and basic understanding on lua.

Welcome, today we'll learn chat commands.

We'll take a look at the Chatted event, which is an event from the player object. You could use whatever way you want to reference the player, but im going to assume you're using local scripts.

What is the Chatted event?

The chatted event is fired when the player says something in the chat, this is done using some lua bindings by the default chat script. It has 2 Parameters:

Message: it's type is string and is the content inside the player's message Recipient (deprecated): it was the player the messaged was whispered to (Due to it being deprecated, I cannot confirm whether this parameter actually works)

What can we use it for?

We can use it to detect when and what a player chats, which will be used for commands in this tutorial.

How are we going to use it?

We are finally at the point everyone was waiting for, how to make a command. First, we'll need to reference the player and set up the chatted event:

local Player = game.Players.LocalPlayer
Player.Chatted:Connect(function(msg, recipient)
    --Our code will be inside here.

Command checking

Now to check if the player typed a command. We can do this after separating our command from the arguments(if there are any)

local commandSplit = string.split(msg, ' ')
local command = commandSplit[1]

What the code does is use string.split() to split the player's message by every space, then assiging commandSplit[1] to the command variable, meaning we get the command. (The string.split() function will split a string into a table, separated by whatever argument you supply) Now, to check if the player actually typed a command, you can use a if statement. (note, replace "comandName" with the name of your command)

if command == "comandName" then
    --code for your command here

Making the command

So, for this tutorial, i'll just make a simple command, for learning, but yours wont be exactly like mine. We'll make a part invisible when the player types the command, and if its already invisible, it will be visible again.

local myPart = game.Workspace.Part --For more efficiency, declare myPart outside of the event.
myPart.Transparency = not myPart.Transparency --

What we do here is we first declare myPart, and then assign it to game.Workspace.Part and then invert the Transparency.

Thats it, you should already have enough to make your own cool commands! If you got lost during the tutorial, here is the whole code we made here.

local Player = game.Players.LocalPlayer
local myPart = game.Workspace.Part
    local commandSplit = string.split(msg, ' ')
    local command = commandSplit[1]
    if command == "comandName" then
       myPart.Transparency = not myPart.Transparency

Thanks for reading my tutorial and i hope you have a fun time making commands with your fresh knowledge!

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