- The code is supposed to take in a message sent by an Admin and handle it and run the specified command and give it the parts of the message as well as the sender of the message.
- When it comes to improving the code I’m not really sure what could improve other than the string handling but I’m unsure of how.
- I’d like to know if this code would be seen as well optimized and efficient and/or if there is something I should think about doing in the future and/or now.
To save runtime I added a spawn() at the System.addToLogs in parseMsg. This way the command runs faster and the log is saved at the same time. Before this, it was just the function without the spawn.
---------------
-- Functions --
---------------
local function checkIfAlias(CommandName)
for _,v in pairs(cmds) do
if v.Name ~= "AdminList" and v.Aliases ~= nil then
for _,k in pairs(v.Aliases) do
if string.lower(k) == CommandName then
return v.Name
end
end
end
end
return nil
end
local function parseMsg(plr, msg)
msg = string.lower(msg)
local PrefixMatch = string.match(msg,"^"..Prefix)
if PrefixMatch then
msg = string.gsub(msg,PrefixMatch,"")
local Arguments = {}
for Argument in string.gmatch(msg, "[^%s]+") do
table.insert(Arguments, Argument)
end
local CommandName = Arguments[1]
table.remove(Arguments, 1)
local Alias = checkIfAlias(CommandName)
if Alias ~= nil then
CommandName = Alias
end
local CommandFunc = cmds[CommandName].Run
if CommandFunc ~= nil then
spawn(function()
System.addToLogs(msg, plr)
end)
CommandFunc(plr, Arguments)
end
end
end
local function isAdmin(plr)
for _,Admin in pairs(cmds.Admins:GetAsync("Admins")) do
if type(Admin) == "number" and Admin == plr.UserId then
return true
end
end
return false
end
------------
-- Events --
------------
Players.PlayerAdded:Connect(function(plr)
print(0)
plr.Chatted:Connect(function(msg, r)
if not r and isAdmin(plr) then
parseMsg(plr, msg)
end
end)
end)
An example of the command table in module script:
cmds.commandName = {
Name = "commandName",
Desc = "commandDescription",
Use = "commadName [args]",
Group = "Owner only/Admin",
Aliases = {"list of aliases, if any"},
Run = function(sender, args)
-- Code goes here
end
}