How To: Make Chat Commands

Hello. Thank you for reading this. This is my first community tutorial, so please leave your feedback below. Anyways, my goal here is to help people achieve making their own chat commands. It’s fairly simple once you understand the most parts. I assume you have a basic understanding of scripting in Roblox, so with that, let’s set out variables.

Variables
local Admins = {""} 
local Prefix = "/"

Now that we got that out of the way, we need to detect when a player joins, and if they are an admin, detect when they chat.

Code
game.Players.PlayerAdded:Connect(function(plr) 
	for _,v in pairs(Admins) do 
		if plr.Name == v then 
			plr.Chatted:Connect(function(msg)
				
			end)
		end
	end
end)

Now with that set, we can finally start making commands! But, what if we don’t want the commands to be case-sensitive? Well, that’s easy. The following code will make your command not case-sensitive.

local loweredString = string.lower(msg)
local args = string.split(loweredString," ")

Now we can ACTUALLY start making commands. In this tutorial, I’ll show you how to make a WalkSpeed command.

Setting up WalkSpeed command
if args[1] == Prefix.."walkspeed" then 
					
end

Now we need to set the player’s WalkSpeed to the second argument. But like we did with the message, we want to make the player’s name not case-sensitive. So add this to your script.

for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		if string.sub(string.lower(player.Name), 1, string.len(args[2])) ==string.lower(args[2]) then
							
   end
end

Now we need to finish up, by setting the WalkSpeed.

Finishing up
player.Character.Humanoid.WalkSpeed = args[3]

Your code should now look line this:

local Admins = {""}
local Prefix = "/" 

game.Players.PlayerAdded:Connect(function(plr)
	for _,v in pairs(Admins) do
		if plr.Name == v then
			plr.Chatted:Connect(function(msg)
				local loweredString = string.lower(msg)
				local args = string.split(loweredString," ")
				if args[1] == Prefix.."walkspeed" then
					for _,player in pairs(game:GetService("Players"):GetPlayers()) do
						if string.sub(string.lower(player.Name), 1, string.len(args[2])) == string.lower(args[2]) then
							player.Character.Humanoid.WalkSpeed = args[3]
						end
					end
				end
			end)
		end
	end
end)

Thank you for taking time out of your day to read this. Please don’t just copy the code, I made this to help people learn. Happy programming!

50 Likes

I think you should put a headline saying that this post assumes that you have a basic understanding of scripting as if you try to explain everything that new scripters don’t know, it would be a very long post.

I advise you to use UserIds, because some admins could change their name, and making the script thinks they are not admins. So use UserId as you cannot change your own UserId.

I’m not the smartest scripter, but you can try to use table.find().

The statement "Just add this to your script.” makes me so mad about this tutorial. Learning is not about copying and pasting. You didn’t even explain what these code lines and just tell us to paste this line. This is the first impression of a bad tutorial.

Can you explain this then?

New scripters already copied and pasted this code line in their script. This is not like a tutorial about how to create admin chat commands. The steps you used and explained are unclear.

I hope this post contributes for you to learn how to create a better tutorial next time, but thanks for the contribution for devforum.

14 Likes

Alright, I’ll do that now.

People get just use game.Players:GetPlayerFromUserId(v) after if plr.Name == v

I did explain it, but thanks for your opinion.

I already explain what it does. It makes the commands not case-sensitive.

Thank you for giving your feedback. This is my most successful post on the devforum. I will try harder on my text tutorial.

2 Likes

So I’ve taken a look, there are actually a lot of chat command resources, and this in my opinion is very basic. But I’ll provide some feedback none the less! :smiley:


First by using this method it prevents the addition of using userid for admins, other admins, for instance group admins or temporary admins. You should change your admin detection line to use a function.

For example

local admins = {12345678, "kingerman88"} 
function checkAdmin(player)
   for i,v in ipairs(admins) do
      if type(v) == 'number' and player.UserId == v then
         return true
      elseif type(v) == 'string' and player.Name == v then
         return true
      end
   end
   if player:GetRankInGroup(1234567) > 5 then -- group id 
     return true
   end
   return false
end

-- We can check the admins with a simple if statement
if checkAdmin(player) then
   -- continue with admin code
end

Next for the way you store commands, I would be against using if else statements, since it will get messy quickly.
I suggest utilizing a table with indexable keys

local commands = {
   [prefix.."walkspeed"] = function(args)
      -- do stuff here
   end,
   [prefix.."command2"] = function()
      --more stuff
   end
}

Otherwise you will have a lot of if then statements and it will be a mess (maybe use module scripts to keep commands clean)


Other than that pretty good entry level tutorial for chat commands :+1:

5 Likes

Thank you for your feedback. I like to use the basic version of making commands just so it makes more sense.

1 Like

@zCrxtix clearly I don’t see where’s the explanation.

1 Like

A cool feature that I dont see getting utilised as much as it should given how useful it is, is the Lua Chat API’s processor functions. Its also recomended that you use this over Chatted as it can be used to completely hide messages from chat.

The Documentation doesn’t explain how to fetch the Chat API, so I’ll leave that here:

local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService))
3 Likes

How would I make this open a UI? My UI code does not work as a localscript in SPS.

local plr = game.Players.LocalPlayer 
plr.Chatted:Connect(function(msg) 
	if msg:lower() == ">>/openPanel" then
		plr.PlayerGui.Panel1.LoginFrame.Visible = true 
	end
end)

Interesting Tutorial, it will definitely help programmers learn how to not use the full name and only use some of the players name. Very helpful. Good job!

Change it to this:

local plr = game.Players.LocalPlayer 
plr.Chatted:Connect(function(msg) 
	if msg:lower() == ">>/openpanel" then
		plr.PlayerGui.Panel1.LoginFrame.Visible = true 
	end
end)

It’s saying “if msg is “>>/openpanel” then…”, but the “p” is capitalized.

1 Like

i already fixed it weeks ago lol

when I run

if checkAdmin(player) then

“player” returns as an error.

Has anyone tried using this Command Module?

2 Likes