I’m trying to make a custom admin system and need to search through a table to see if the command exists. The script works perfectly fine when the command exists but when it doesn’t it breaks and sends errors on the lines with table.find().
Here is the code:
local function searchCommands(cmd)
--Check Commands
for _, command in pairs(commands) do
if table.find(command.Commands, cmd) then
return command
end
end
--Check CustomCommands
for _, command in pairs(customCommands) do
if table.find(command.Commands, cmd) then
return command
end
end
return false
end
The table is a module script with entries formatted like this:
module.ExampleCommand = {
Prefix = settings.Prefix;
Commands = {"example";"examplecommand"};
Args = {"Example argument"};
Description = "This is an example command.";
Hidden = true;
Fun = false;
AdminLevel = 0;
Function = function(plr,args)
print("This is an example command!")
print("This command was sent by "..plr.Name)
print(args[1])
end
}
I figured that was it. The command table doesnt have a Commands key which results in that error. You could use short circuiting to prevent that by checking if the command table has the Commands key.
replace this:
if table.find(command.Commands, cmd) then
with this:
if command.Commands and table.find(command.Commands, cmd) then