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

Before you read this tutorial, I really recommend you read the first 2 before reading this one:
How To Make Admin Commands, A More In-Depth Guide: Part 1
How To Make Admin Commands, A More In-Depth Guide: Part 2

Anyways, in this tutorial I’ll be going over data handling, and making a ban command with that data. First, let’s create a ModuleScript inside of our Modules folder, and name it Data. This is where we will handle the rank and their ban data. Inside of that script, type this.

Data Handling


local DATA_TEMPLATE = { --the template of our data, whats given to every player
	Ban = {
		Banned = false,
		Reason = "N/A",
		Moderator = nil
	},
	Level = 0
}
local data = {}
local data_cache = {} --where our data for each player will be held

local DS = game:GetService("DataStoreService"):GetDataStore("AdminDS")
local Players = game:GetService("Players")

function data.onPlayerAdded(player) --load the data in data_cache
	local data = DS:GetAsync("UID_"..player.UserId) or DATA_TEMPLATE

	for index, value in pairs(DATA_TEMPLATE) do
		if not data[index] then
			data[index] = value
		end
	end

	data_cache[player] = data
end
function data.get(player) --get data
	return data_cache[player]
end

function data.set(player) --update data
	local success, err = pcall(function() DS:SetAsync("UID_"..player.UserId, data_cache[player]) end)
	if not success then
		print(err)
	end
end


return data

We have 3 functions here: onPlayerAdded, get, and set. onPlayerAdded is what loads the data when the player joins, get returns the data of the player in data_cache, and set updates the values after we change them. Now, we’ll work on our ban command.

Notice: I think it’s pretty risky working with data stuff, especially if you don’t have a plugin that can erase/change your data. If you ban yourself for testing, how would you unban yourself? You should install this plugin. Although it’s 2 dollars, it’s worth it.

Ban Command


Now, lets set up our ban command. Go into your Commands module and add this.

{
		name = "ban",
		aliases = {},
		category = "moderation",
		level = 2,

		execute = function(player, args)
			for _,v in pairs(GetPlayer(player, args)) do --get player
				if not args[2] then return end --if we dont know who to ban, then break out of the function
				if not args[3] then
					local data_ = data.get(player) --get data
					if data_.Ban.Moderator == nil then data_.Ban.Moderator = player.Name end --set data
					if not data_.Ban.Reason then data_.Ban.Reason = "N/A" end
					data_.Ban.Banned = true
					data.set(player)
					v:Kick("You were kicked from the game.\nModerator: "..data_.Ban.Moderator..".\n\nReason:\n"..data_.Ban.Reason) --kick
				else
					local data_ = data.get(player)
					local result = {}
					for i = 3, #args do
						table.insert(result, args[i])
					end
					if data_.Ban.Moderator == nil then data_.Ban.Moderator = player.Name end --set data
					if data_.Ban.Reason == "N/A" then data_.Ban.Reason = table.concat(result, " ") end
					data_.Ban.Banned = true
					data.set(player)
					print(data_)
					v:Kick("You were kicked from the game.\nModerator: "..data_.Ban.Moderator..".\n\nReason:\n"..data_.Ban.Reason)
				end
			end
		end,
	}

Make sure after every command you add in {}s, seperate them by a comma.

What we’re doing here is if we don’t know who to ban, break out of the function. If the reason isn’t defined, update their data and set their reason to "N/A", kick them. If the reason is defined, get all of the arguments, and update their ban info, kick them.

You can add as many commands as you wish. If you have any questions, comments or ideas, feel free to comment them or send me a pm. Good luck on building your very own admin system!

Should I make a part 4 that goes over guis and logging?
  • Yes
  • No

0 voters

5 Likes