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!
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
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
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
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