How To Make Admin Commands, A More In-Depth Guide: Part 2

In the previous tutorial, we went over how commands are parsed, and setting up the main parts. So far, your setup should look something like this.
image

Inside the Main script, add this code.

return function(con)
    game.Players.PlayerAdded:Connect(function(player)
	    for _, v in pairs(config["Banned"]) do --ban handling, *optional*
		    if player.Name == v or player.UserId == v then
			    player:Kick("You are banned!")
		    end
	    end
     --execute commands
     player.Chatted:Connect(function(msg)
	    if msg:sub(1,1) == config["Prefix"] or msg:sub(1,1) == ":" then
			    msg = msg:sub(2) --break off the first part of the message
		        local args = string.split(msg, " ")
			    local commands = require(script.Parent.Modules.Commands) --we'll add this next
		    end
	    end)
    end)
end

What we’re doing here is parsing the command, which I explained in part 1. Now inside of the Modules folder, create a ModuleScript called Commands. Now type this.

return { --return a table of all of the commands
	{
		name = "walkspeed", --name of command, whats used to execute it
		aliases = {"speed", "ws"}, --other words used to execute it
		level = 1, --admin level. 0 = player, 1 = moderator, 2 = admin, 3 = owner (at least in my system, you're free to add more like vip and headadmin)

		execute = function(player, args) --the function that runs when called
			
		end,
	}

}

That is the basic template of a command. Now, let’s go back into our Main script, and execute the command!

return function (con)
	local config = require(con)
	game.Players.PlayerAdded:Connect(function(player)
		for _, v in pairs(config["Banned"]) do --ban handling, *optional*
			if player.Name == v or player.UserId == v then
				player:Kick("You are banned!")
			end
		end
		--execute commands
		player.Chatted:Connect(function(msg)
			if msg:sub(1,1) == config["Prefix"] or msg:sub(1,1) == ":" then
				msg = msg:sub(2) --break off the first part of the message
				local args = string.split(msg, " ")
				local commands = require(script.Parent.Modules.Commands) --we'll add this next
				for i,command in pairs(commands) do
				if args[1]:lower() == command.name then
					command.execute(player, args)
				end
				for i,alias in pairs(command.aliases) do
					if args[1]:lower() == alias then
							command.execute(player, args)
						end
					end
				end
			end
		end)
	end)
end

Now, writing the function to execute it.

if args[3] then
	for _,v in pairs(GetPlayer(player, args)) do --GetPlayer function is explained next
		v.Character.Humanoid.WalkSpeed = tonumber(args[3])
	end
end	

The GetPlayer function returns the player. Instead of "me", it would return the player. Or instead of "all", it would return every player in the game. Keep in mind that when you return something, it ends the function. You can put it in another module, or put it in a function above.

function GetPlayer(player, args)
    if args[2] then
	    if args[2] == "me" then
			return {player}
		elseif args[2] == "all" then
			local players = {}
			for _,v in pairs (game.Players:GetPlayers()) do
				table.insert(players, v)
			end
			return players
		else
			for _,v in pairs(game.Players:GetPlayers()) do
				if string.sub(string.lower(player.Name), 1, string.len(args[2])) == string.lower(args[2]) then
					return {v}
				end	
			end
		end
	end
	return {player}
end

Read next part: here

I hope instead of copying the code, you learn something new. I enjoy making these tutorials. Please leave any questions, feedback, or ideas. Thank you for reading!

12 Likes

THANK YOU SO MUCH @zCrxtix!!! it worked

1 Like