Chat Admin Commands

I am trying to make a Admin script using chat commands, I have these commands setup in a module which I will show below, the issue I am having is how I could make it so that the admins would only have to type increments of the players names, and to get it to read and go by whatever the command requires
ex: :Kick(player,victim) would get the player initiating the kick and victim is the one being kicked, that kind of thing. I haven’t been able to find anything on here that is like this so I also think providing this would help a lot of people out including myself.

Here is the current code I have, which gets when the player chatted and if they are an admin it will run to the commands if they have typed one:

local Admins = {
    "Player1"; -- Username example
    "Player2"; -- Username example
    
    {GroupId = 4525406;
    RankId = 250;} -- Group example
}

local Prefix = ":"

local Players = game:GetService("Players")

local commandModule = require(script.Commands)

commandModule.print = function(Sender,Arguments)
    local Message = table.concat(Arguments," ")
    print("From " ..Sender.Name..":\n"..Message)
end

local function IsAdmin(Player)
    for _,Admin in pairs (Admins) do
        if type(Admin) == "string" and string.lower(Admin) == string.lower(Player.Name) then
            return true
        elseif type(Admin) == "number" and Admin == Player.UserId then
            return true
        elseif type(Admin) == "table" then
            local Rank = Player:GetRankInGroup(Admin.GroupId)
            if Rank >= Admin.RankId then
                return true
            end
        end
    end
    return false
end

local function ParseMessage(Player,Message)
    --Message = string.lower(Message)
    local PrefixMatch = string.match(Message,"^"..Prefix)

    if PrefixMatch then
        Message = string.gsub(Message,PrefixMatch,"",1)
        local Arguments = {}

        for Argument in string.gmatch(Message,"[^%s]+") do
            table.insert(Arguments,Argument)
        end

        local CommandName = Arguments[1]
        table.remove(Arguments,1)
        local CommandFunc = commandModule[CommandName]
        
        if CommandFunc ~= nil then
            CommandFunc(Player,Arguments)
        end
    end
end

Players.PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Message,Recipient)
        if not Recipient and IsAdmin(Player) then
            ParseMessage(Player,Message)
        end
    end)
end)```

here is the module with the commands:

```lua
local Commands = {
    
    ban = function(player, victim)
        
        local DataStoreService = game:GetService("DataStoreService")
        local BanDataStore = DataStoreService:GetDataStore("BanData")
        
        local success, errormessage = pcall(function()
            BanDataStore:SetAsync(victim.UserId, true)
        end)
        
        if success then
            print "Player is now banned."
        end
        
        victim:Kick("You have been permanently banned.")
        
    end;
    
    kill = function(player, victim)
        
    end;
    
    to = function(player, victim)
        player.Character:MoveTo(victim.Character.Head.Position)
    end;
    
    bring = function(player, victim)
        victim.Character:MoveTo(player.Character.Head.Position)
    end;
    
    grow = function(player, victim, value)
        if victim.Character:FindFirstChild("Growth") then
            victim.Character.Growth.Value = value
        end
    end;
    
}

return Commands```

Thank you in advance!
4 Likes

Just wrote this and tried it in studio and it worked for me so see if it works and try to adapt it to your game.

local splittedString = string.split(":ban 112" ," ") 
if string.sub(splittedString[2]:lower(),1) == string.sub(game.Players.LocalPlayer.Name:lower(),1,splittedString[2]:len()) then 
	print("Found") 
end

Where do I put that? I am not too experienced with the code you used.

In the parse message function where you check the message also change the first part of string.split.

What does this do exactly? I am needing it to link to the req variables of whichever command is given, and to also find the player names.

It does what you wanted it works if its abbreviated and also if its the whole player’s name and whats the req variable?

Refer to the commands, notice how some call for player and victim, others call for values as well.

It would be helpful if you could add it into the code as well, especially for anyone that may view it.

string.split(x," ") is a table with every word inside of it so if we had:

→ string.split(“foo bar x”, " ")

We would have:

→ foo
→ bar
→ x

So that deals with the extra values and stuff.

This would be it assuming that the 2nd word in the message is the victim’s name.

local splittedString = string.split(Message," ") 
if string.sub(splittedString[2]:lower(),1) == string.sub(victim.Name:lower(),1,splittedString[2]:len()) then 
	print("Found") 
end

I don’t understand where you want me to put this.

local function ParseMessage(Player,Message)
	--Message = string.lower(Message)
	local PrefixMatch = string.match(Message,"^"..Prefix)

	if PrefixMatch then
		Message = string.gsub(Message,PrefixMatch,"",1)
		local Arguments = {}

		for Argument in string.gmatch(Message,"[^%s]+") do
			table.insert(Arguments,Argument)
		end

		local CommandName = Arguments[1]
		table.remove(Arguments,1)
		local CommandFunc = commandModule[CommandName]
		
		if CommandFunc ~= nil then
			CommandFunc(Player,Arguments)
		end
	end
end
local function ParseMessage(Player,Message)
	--Message = string.lower(Message)
	local PrefixMatch = string.match(Message,"^"..Prefix)

	if PrefixMatch then
		Message = string.gsub(Message,PrefixMatch,"",1)
		local Arguments = {}

		for Argument in string.gmatch(Message,"[^%s]+") do
			table.insert(Arguments,Argument)
		end

		local CommandName = Arguments[1]
		table.remove(Arguments,1)
		local CommandFunc = commandModule[CommandName]
        local splittedString = string.split(Message," ") 
        if string.sub(splittedString[2]:lower(),1) == 
             string.sub(victim.Name:lower(),1,splittedString[2]:len()) then 
	         print("Found") 
        end    		

		if CommandFunc ~= nil then
			CommandFunc(Player,Arguments)
		end
	end
end

Victim is a nil variable, where are you getting it from?

I saw victim in your script so i put it there anyway edit it to work for your game.

Victim is only in the commands, which is in a separate module.

Oh okay then put in the commands and edit it i thought ParseMessage handled every command.

wdym put it in the commands? it’s already in there.

Well where are you handling the abbrevation.

here is the code again:

local Admins = {
	"Player1"; -- Username example
	"Player2"; -- Username example
	
	{GroupId = 4525406;
	RankId = 250;} -- Group example
}

local Prefix = ":"

local Players = game:GetService("Players")

local commandModule = require(script.Commands)

commandModule.print = function(Sender,Arguments)
	local Message = table.concat(Arguments," ")
	print("From " ..Sender.Name..":\n"..Message)
end

local function IsAdmin(Player)
	for _,Admin in pairs (Admins) do
		if type(Admin) == "string" and string.lower(Admin) == string.lower(Player.Name) then
			return true
		elseif type(Admin) == "number" and Admin == Player.UserId then
			return true
		elseif type(Admin) == "table" then
			local Rank = Player:GetRankInGroup(Admin.GroupId)
			if Rank >= Admin.RankId then
				return true
			end
		end
	end
	return false
end

local function ParseMessage(Player,Message)
	--Message = string.lower(Message)
	local PrefixMatch = string.match(Message,"^"..Prefix)

	if PrefixMatch then
		Message = string.gsub(Message,PrefixMatch,"",1)
		local Arguments = {}

		for Argument in string.gmatch(Message,"[^%s]+") do
			table.insert(Arguments,Argument)
		end

		local CommandName = Arguments[1]
		table.remove(Arguments,1)
		local CommandFunc = commandModule[CommandName]
		local splittedString = string.split(Message," ") 
		if string.sub(splittedString[2]:lower(),1) == 
			string.sub(victim.Name:lower(),1,splittedString[2]:len()) then 
			print("Found") 
		end    		

		if CommandFunc ~= nil then
			CommandFunc(Player,Arguments)
		end
	end
end
Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message,Recipient)
		if not Recipient and IsAdmin(Player) then
			ParseMessage(Player,Message)
		end
	end)
end)


here is the module:

```lua
local Commands = {
	
	ban = function(player, victim)
		
		local DataStoreService = game:GetService("DataStoreService")
		local BanDataStore = DataStoreService:GetDataStore("BanData")
		
		local success, errormessage = pcall(function()
			BanDataStore:SetAsync(victim.UserId, true)
		end)
		
		if success then
			print "Player is now banned."
		end
		
		victim:Kick("You have been permanently banned.")
		
	end;
	
	kill = function(player, victim)
		
	end;
	
	to = function(player, victim)
		player.Character:MoveTo(victim.Character.Head.Position)
	end;
	
	bring = function(player, victim)
		victim.Character:MoveTo(player.Character.Head.Position)
	end;
	
	grow = function(player, victim, value)
		if victim.Character:FindFirstChild("Growth") then
			victim.Character.Growth.Value = value
		end
	end;
	
}

return Commands

Where are you handling the abbrevation.

There isn’t one, it should be in ParseMessage though, that’s where it handles that.