"Custom Command" System

So, i’ve seen all these admin systems, such as Adonis and SimpleAdmin, use “custom command” systems, where you have a folder and you edit a certain modulescript based on your CC needs.

I want to create something like that, but I don’t know where to start.

How would I go about doing this?

Thanks,
South

1 Like

The TextChatService has recently gone out of beta, so you can use that. Go to the Model tab and click on “Service”. You can insert TextChatService from there. Next, insert a TextChatCommand into TextChatService. Give it a primary and optional secondary alias in the properties. You can connect to an event that is fired when that command is set off. Here’s an official example:

--!strict
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local Command: TextChatCommand = TextChatService:WaitForChild("TextChatCommand")

Command.Triggered:Connect(function(textSource, message)
	local player = Players:GetPlayerByUserId(textSource.UserId)
	
	if player then
		local character = player.Character
		
		if character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			
			if humanoid then
				for _, child in ipairs(humanoid:GetChildren()) do
					if child:IsA("NumberValue") then
						TweenService:Create(child, TweenInfo.new(), { Value = child.Value * 2}):Play()
					end
				end
			end
		end
	end
end)

If you don’t like this bleeding-edge method, you can resort to listening to the Player.Chatted event like this:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        -- Parse your command here
    end)
end)

What does that have to do with anything?

Also, im looking to use modulescripts

Sorry. I didn’t read that you wanted the module script method. Are you looking for something more like this?

local Commands = require(game.ReplicatedStorage.Modules.Commands)

Commands.Create("setname", function(player: Player, name: string)
	local char = player.Character
	if char then
		char.Humanoid.DisplayName = name
	end
end)

If so, I can make a quick module out of it.

YES!

That’s exactly what i’m looking for!

Done. Here is the module:

--!strict

local Commands = {
	__ = {
		Commands = {} :: { [string]: (Player, ...string) -> () },
		Event = nil :: RBXScriptConnection?
	}
}
Commands.__index = Commands

export type Type = typeof(setmetatable({}, Commands))

function Commands.new(): Type
	local t = setmetatable({}, Commands)
	t.__.Event = game.Players.PlayerAdded:Connect(function(player: Player)
		player.Chatted:Connect(function(msg: string)
			t:OnPlayerChatted(player, msg)
		end)
	end)
	return t
end

function Commands:Add(name, func)
	self.__.Commands[name] = func
end

function Commands:Remove(name)
	self.__.Commands[name] = nil
end

function Commands:OnPlayerChatted(player: Player, msg: string)
	local params = {}

	for param in string.gmatch(msg, "([^ ]+)") do
		table.insert(params, param)
	end
	
	for name, func in pairs(self.__.Commands) do
		print(params)
		if params[1] == name then
			func(player, table.unpack(params, 2))
		end
	end
end

return Commands

And here is an example “/damage” command:

--!strict

local Commands = require(Commands).new()

Commands:Add("/damage", function(player: Player, dmg: string)
	local dmgnum = tonumber(dmg)
	if not dmgnum then return end
	local dmgnum: number = dmgnum :: number -- Roblox type deduction at its finest
	
	local char = player.Character
	if char then
		local hum = char:FindFirstChildOfClass("Humanoid")
		if hum then
			hum.Health -= dmgnum
		end
	end
end)
2 Likes

Thanks so much!

I’ll definitely use it!