Creating an Admin Command
Creating an Admin Command
An admin command is a common feature in Roblox games where a player can type a command into the chat window which will trigger a game action. In this tutorial we will examine how the Chat System can be modified to listen for a specific message, an admin command, and then trigger an in-game action that we create.
The Chat System in Roblox is, by default, a Roblox-provided collection of scripts that give a game its chat functionality. The Chat System is important because it connects any message that a user types in the Roblox chat window to actual events, whether that be filtering the message and displaying it to other users or triggering an in-game action. In this tutorial, we will use a ChatModule and
Explosion to make a player’s character explode when they type an admin command.
- Roblox Studio
- Basic Roblox lua
- Scripts, functions, tables
- Parent-child hierarchy
There are a number of ways to implement admin commands, but one of the most straightforward is to use ChatModules. ChatModules listen to incoming messages on the server and can execute actions based on whatever criteria is desired. In this case, we will create an explosion if the player typed a specific string.
Let’s get started with adding an admin command into your own game. The first step that you will need to follow is to add a custom Chat Module to the Chat service. The Chat service itself is the essential storage unit for the chat system. When a Roblox place loads (either in Client or in Studio when running or playing), all of the components of the chat system are automatically loaded into the Chat service. This includes various different scripts and folders, but the most important for adding chat commands is a folder called ChatModules. Modules in this folder are used by the server to process chat messages and determine if they should be treated as a command.
To modify or customize the Chat system, it is recommended to make a copy of the entire service through the following steps:
- Run Studio with the Play button
- Copy the objects in the Chat service
- Press the Stop button
- Paste them into the Chat service
Now add a new
ModuleScript within the ChatModules folder of the
Articles/lua chat system customize your game s chat |Chat Service and name it ExplosionCommand.
ModuleScripts within the ChatModules folder are expected by the chat system to follow a certain format. Each module declares a local function named Run, which takes a ChatService object as its only parameter and is triggered when the game loads and the chat system’s scripts are initialized. The Run() function is then returned by the module at the end of the script. Inside of Run other functions can be registered to the chat system to be called whenever a player types a message. Command functions like these must be registered with ChatService using ChatService’s
RegisterProcessCommandsFunction takes two arguments: a functionId string and a function. The function is what is called when a player chats, and the functionId is an identifier for the function (this identifier is mainly used to remove the binding later if desired). The function should return true if the message should be allowed to continue being processed by the chat service (to be run through other command functions and ultimately be displayed), or false if the chat service should stop processing the message. The function that is registered is passed three arguments whenever it is called on a message: the name of the speaker, an object containing the message itself, and the name of the channel that the message was said in.
local function Run(ChatService) local function processMessage(speakerName, messageObject, channelName) -- Code goes here end ChatService:RegisterProcessCommandsFunction("explosion_command", processMessage) end return Run
In this example, an admin command should be triggered when a player types “/explode” in the chat. The function that we will be writing should return true if both the command was valid and an explosion was created successfully; otherwise, the function should return false. By returning true the chat message will be stopped from proceeding and no message will be displayed in the chat window. If there is no explosion, or if the player didn’t type in the correct command, this function needs to return false so that the message can continue working through the system.
For our explosion command, the processMessage function will need to check several things. First, we need to check if the message that was chatted matches the keyword for our command, in this case “/explosion”:
local messageText = messageObject.Message local isACommand = string.find(messageText, "/explode") if not isCommand then return false end
Next, we need to get the character model of the speaker, as we will be using that to position the explosion. We can use the GetSpeaker function of ChatService to get the speaker object from the name of the speaker, and then GetPlayer to get the Player object.
local speaker = ChatService:GetSpeaker(speakerName) local player = speaker:GetPlayer() local character = player.Character
To keep things nice and organized, we will be relying on a helper function to create an explosion within the player’s character model. Our helper function will be called makeExplosion and will return true or false depending on whether or not it executed successfully. The helper function makeExplosion will take a player’s character model as its only parameter.
local success = makeExplosion(character) return success
First, makeExplosion will check if the passed in character object exists and if that character has a HumanoidRootPart, which is where we will be inserting and positioning the explosion. If a HumanoidRootPart exists then makeExplosion will make our explosion, and return true. Otherwise, makeExplosion will return false.
local function makeExplosion(character) if character and character:FindFirstChild("HumanoidRootPart") then local explosion = Instance.new("Explosion") explosion.Position = character.HumanoidRootPart.Position explosion.Parent = character.HumanoidRootPart return true end return false end
If makeExplosion executes successfully then the explosion will be created and the function can return true having processed the message, otherwise, it will return false. Altogether, our ModuleScript should contain the following.
local function makeExplosion(character) if character and character:FindFirstChild("HumanoidRootPart") then local explosion = Instance.new("Explosion") explosion.Position = character.HumanoidRootPart.Position explosion.Parent = character.HumanoidRootPart return true end return false end local function Run(ChatService) local function processMessage(speakerName, messageObject, channelName) local messageText = messageObject.Message local isACommand = string.find(messageText, "/explode") if not isACommand then return false end local speaker = ChatService:GetSpeaker(speakerName) local player = speaker:GetPlayer() local character = player.Character local success = makeExplosion(character) return success end ChatService:RegisterProcessCommandsFunction("explosion_command", processMessage) end return Run