I want to learn how to make to custom admin commands for my free admin game
Custom admin is a lot more simple than many originally expect.
- You will begin by using the
Player.Chatted
connection. Make sure you retrieve the message output aswell, via Player.Chatted:Connect(function(msg) ->code end) - Now we have a detection for somebody typing a message, and we have their message.
At this point, you’ll need a dictionary or other sort of table.
- The easiest way to do this is make a modulescript, as its organized for you.
Module Script Example
local module = {
Settings = {
Prefix = ":",
Ranks = {
[1] = "Mod",
[2] = "Admin",
[3] = "Owner",
}
}
Commands = {
"Kick" = function(target) target:Kick() end,
"Kill" = function(target) target.Character.Humanoid.Health = 0 end,
}
}
return module
Great! We have a way to make commands and maybe some settings for later. I won’t include those in this summary, however.
Now, we need to interpret the message.
- First, check if it has a : (colon) which is the prefix of my module from earlier.
- Then, we will just check if the message contains a command!
Script Example
local module = require(script.Module) -- assume module is a child of this script
game:GetService("Players").PlayerAdded:Connect(function(p)
p.Chatted:Connect(function(msg)
if msg and string.sub(msg, 1, 1) == module.Settings.Prefix then -- check that the message has the prefix
local splitmsg = string.split(string.gsub(msg, module.Settings.Prefix, ""), " ") -- eliminate spaces. This will return a message like :Kick Squid to {"Kick", "Squid"}
for cmd, func in module.Commands do
if string.lower(splitmsg[1]) == string.lower(cmd) then
local foundplayer = game:GetService("Players"):FindFirstChild(splitmsg[2])
if foundplayer then
pcall(function() func(foundplayer) end)
end
end
end
end
end)
end)
This is the baseline for a working system. Feel free to ask questions, or let me know if there are errors.
Well first let’s break down what an admin command is.
Generally it’s a prefix (so we know it’s a command and not just a chat message). Often this prefix is a slash or a colon.
So first thing we do is hook up to chat. Then we check if the first #prefix chars = prefix so we know it’s a command and to continue processing.
Next we have command words. Basically the command type to execute. Usually this and all of the properties are space separated, so we need to split our string by spaces or whatever delimiter you chose.
So now we have the command in a form similar to this
[“tp”, “player1”, “player2”] (note I removed the prefix)
Built from the command
“/tp player1 player2”
So now we go part by part and look up what’s expected. So for example we know that the tp command wants a player and another player. So we can look up in our command list (in the form of a dictionary) what those arguments are supposed to be for this command. We can then turn those properties to their correct type like player and finally pass them into a function tied to the command name.
Then just write that function however you need.
This is of course just one way to go about it. Sometimes commands might be more complex or have multiple valid forms. You can come up with some data structure to represent that or make a command take a string and process command specific logic internally. You also need decoders for like the player properties so you can find the player correctly. Often this is searching for the first string that starts with what you had typed and optionally ensuring there isn’t more than one option.
thx for the example script! I’ll be able to apply this
Is there any thing else I need to about when making admin commands?
Is there anything else I need to know about when making admin commands?
I’ll might be posting more questions about this as long as this post stay open for a few days
A formal Roblox CLI (Command Line Interface) would utilize string patterns to break down the input. An initial pattern is applied to the input to simultaneously verify the presence of an instigator prefix and separate the command from its arguments. Commands are ideally implemented in individual ModuleScript
s. In these scripts, you will specify the command name, its aliases, its prerequisites, an argument parser, and the command functionality. The CLI core will locate the command by its name or alias, verify prerequisites, then run its argument parser on the previously isolated input component. The parser will use additional string patterns to break down the isolated input, and will produce tangible values. These values are then passed to the command function, and the command is executed. It’s likely that multiple commands will require similar inputs, so it’ll be wise to create a repository of standard argument parsers which can be assigned to the command
A great example of this high-level implementation is Cmdr. I recommend taking inspiration from its design
It’s saying inside the commands that “assigning 2 values to 1 variables leaves some values unused”
You’ve implemented something incorrectly. Variables can only hold one value. You’re likely writing:
local x = 1, 2
This would only store 1, losing 2
Ok, I’ve asked the assistant two times for this solution they both didn’t show any errors
I can use either commas or []
I like this guide, teach me more about them!
wery cool explanation! Thanks for making it so understandable :D.
This is outdated. The Player.Chatted event is now a legacy event. Roblox has defaulted all new experiences to use the new chat system, which does not incorporate that event.
but still you get the idea of how to do admin commands. Right?
Sure, but the overall approach is crude
whats the better way to make admin commands then?
My mistake, I haven’t touched on it in a while
For a forum question there’s no reason to give anything more than that, if he wanted a flawless system he will work and create it in time.