Admin System won't run

I want the admin system I am creating to run the command if a command name is found within the message (i.e. :kick) with the arguments provided.
The commands won’t run and is not throwing any errors.
Not much since this is a custom admin system. I have not looked on the DevForum for answers.

This is what it looks like in the explorer:
image

"Admin" code
local commands = {}
local admins = {}
local ranks = {}
local services = require(script.Modules.Services)
local commandsfolder = script.Commands
local config = require(script.Configuration)


local function CheckAdmin(player)
    if admins[player.Name] then
        return true
    elseif not admins[player.Name] then
        return false
    end
end

local function CheckRank(player)
    if admins[player.Name] then
        return admins[player.Name]
    end
end


for a,b in next,script.Commands:GetChildren() do
    if b:IsA("ModuleScript") then
        commands[b.Name] = require(b)
        end
end

for key, value in pairs(config.Admins) do
    admins[value] = true
end

for key, value in pairs(config.Ranks) do
    ranks[value] = true
end

services.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg, rec)
        if CheckAdmin(player) then
            local command, targetPlayerName = msg:match("^/(%S+)%s+(%S+)")

            if command and commands[command] then
                local targetPlayer = game.Players:FindFirstChild(targetPlayerName)

                if targetPlayer then
                    commands[command](player, targetPlayer)
                else
                warn("Invalid Command ran from "..player.Name..": "..command)
                end
            end
        end
    end)
end)
"Kick" code
return function(player,targetplayer)
    targetplayer:Kick(player.Name.." has kicked you from the server!")
end
"Services" code
local Services = {
    Players = game:GetService("Players"),
    TeleportService = game:GetService("TeleportService"),
    Teams = game:GetService("Teams"),
    DatastoreService = game:GetService("DataStoreService"),
    TestService = game:GetService("TestService"),
    TextChatService = game:GetService("TextChatService"),
    MarketPlaceService = game:GetService("MarketplaceService")
}


return Services
"Configuration" code
local Config = {
    Prefix = ":",
    AutoKickBan = true,
    AutoBanned = {"USERNAMEHERE","USERNAMEHERE2","USERNAMEHERE3"},
    Ranks = {
        Owner = {5},
        HeadAdmin = {4,{}},
        Admin = {3,{}},
        Mod = {2,{}},
        VIP = {1,{}},
        Player = {0,{}},
    };
    
    Admins = {
        ["Jerold09362"]= 5,
    }
}

--[[ CONFIG HELP

Prefix is the first character that seperates a message from a command. For example, the ':' in :kick bob.

AutoKickBan is whether or not the game will kick/ban people who are in the AutoBanned table.

AutoBanned is a list of people that will be kicked/banned automatically.

Ranks is a list of people and their ranks. The higher the rank, the more commands they will have access to.

Admins is a list of people who will have access to admin commands.
[RANKNUMBER] = "PLAYERNAME"
]]

All help is appreciated.

1 Like

Why are you using a seperate module for each command? It seems a lot more complicated having different modules for services and config.



Here’s another system you could try:

  • Have a module of all the commands, in ServerScriptService.
  • Have a script to manage how each command is run.

Main Script (Script in ServerScriptService):

local ssService = game:GetService("ServerScriptService")
local players = game:GetService("Players")

local prefix = ":" --commands prefix
local admins = {} --admin usernames
local adminIds = {} --admin UserIds

local commandsScript = require(--[[path to module here]])

local function processMessage(player:Player)
	if not table.find(adminIds, player.UserId) then return nil end
	if not table.find(admins, player.Name) then return nil end
	
	player.Chatted:Connect(function(message)
		local splitMessage = message:split(" ")
		local targetCmd = splitMessage[1]:split(prefix)
		
		if commandsScript[targetCmd[2]] then
			table.remove(splitMessage, 1)
			if table.find(admins, player.Name) and table.find(adminIds, player.UserId) then --extra security idk why i put it here
				commandsScript[targetCmd[2]](player, table.unpack(splitMessage))
			end
		end
	end)
end

players.PlayerAdded:Connect(processMessage)

This will act as a handler when the player chats a command.


Then, in the module, something like:

--bare in mind the arguements will come through as strings. You need to get the target player object.
local players = game:GetService("Players")

local function getTarget(name)
    return players:FindFirstChild(name)
end

--commands
local commands = {}
commands.__index = commands

function commands.kick(admin, target, message)
    target = getTarget(target)
    if target then
        target:Kick(message)
    end
end

return commands
1 Like

hey, so it seems that you didnt really debug ur code
because i found these problems in your code:

when setting the values for admins u use key and not value

for key, value in pairs(config.Admins) do
	admins[key] = true
end

this is the correct way to string match

local command, targetPlayerName = msg:match("^:(%S+)%s*(%S*)")

i also made sure to make both registered commands and the command being checked
lower case to see if they match eachother like so:

for a,b in next,script.Commands:GetChildren() do
	if b:IsA("ModuleScript") then
		commands[string.lower(b.Name)] = require(b) -- we make the b.name to lower 
	end
end

here is how it’s implemented in the match condition:

local command, targetPlayerName = msg:match("^:(%S+)%s*(%S*)")
command = string.lower(command) -- example: "KiCK" -> "kick"
			 
if command and commands[command] then
    ...
end

here’s the full working code:

local commands = {}
local admins = {}
local ranks = {}
local services = require(script.Modules.Services)
local commandsfolder = script.Commands
local config = require(script.Configuration)


local function CheckAdmin(player)
	if admins[player.Name] then
		return true
	elseif not admins[player.Name] then
		return false
	end
end

local function CheckRank(player)
	if admins[player.Name] then
		return admins[player.Name]
	end
end

for a,b in next,script.Commands:GetChildren() do
	if b:IsA("ModuleScript") then
		commands[string.lower(b.Name)] = require(b)
	end
end

for key, value in pairs(config.Admins) do
	admins[key] = true
end

for key, value in pairs(config.Ranks) do
	ranks[value] = true
end

services.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg, rec)
		if CheckAdmin(player) then
			local command, targetPlayerName = msg:match("^:(%S+)%s*(%S*)")
			command = string.lower(command)
			 
			if command and commands[command] then
				local targetPlayer = game.Players:FindFirstChild(targetPlayerName)

				if targetPlayer then
					commands[command](player, targetPlayer)
				else
					warn("Invalid Command ran from "..player.Name..": "..command)
				end
			end
		end
	end)
end)

hope it works, if it does i’d appreciate if u solution it! :smile:

1 Like

Both worked.

Unsure how I can solution two posts.

Also, thank you both for your help.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.