Okay so I’m in the process of creating a custom dev console, and part of it is a table that has a list of allowed commands. However the player has to input a command and its corresponding parameter, and I thought of doing this as a way to find the text from the table:
local commands = {
"kill",
}
local textthere = base.Main.CommandBar.Input.Text
local historytext = base.Main.History.TEMPLATE:Clone()
historytext.Parent = base.Main.History
historytext.Visible = true
historytext.Text = IDTemplate .. textthere .. " [ ATTEMPTING ]"
print(textthere)
wait(2)
if table.find(commands, string.find(textthere, "kill")) then
print('command found!')
if textthere == string.find(textthere, "kill") then
print('kill is inside the command')
end
historytext.Text = IDTemplate .. textthere .. " [ SUCCESS ]"
else
historytext.Text = IDTemplate .. textthere .. " [ FAILED | COMMAND NOT FOUND ]"
end
So for example, if I were to type “kill joey” in the console, it would search that string for the term ‘kill’ to see if its a valid command. The second if statement is there to double check, and to add its outcome to the script (which would be killing joey).
Problem here, is that it says the command isn’t found. Am I using “table.find(commands, string.find(textthere, “kill”))” wrong?
EDIT: I realize my explanation was a bit confusing. So they have to enter two part of a command, the command itself and the parameter. So the typed command would be “Kill joey”. Well, that obviously isn’t inside the commands table. Which is why I’m searching for “Kill” inside the string a second time at the start.
Let’s say "kill" is present in textthere, making string.find(textthere, "kill") return two numbers.
Using table.find(), you are supposed to use a string in the second parameter in your case. Instead, you’re using a number string.find(textthere, "kill")
btw I have made a functional dev console before! here
Edit: string.find() returns two numbers, not a boolean, my bad lol
Basically what this line of code is trying to do is to search the command table to find a string that is being searched. Yeah, I don’t know if this makes sense. I think the reason why the command isn’t found is because it’s too late then.
For getting the command and it’s arguments, I suggest using the string.split function.
local commands = {
["kill"] = function(target)
-- code
end,
["tp"] = function(target)
-- code
end
}
local words = string.split(text:lower(), " ")
local command = words[1]
if #words > 0 and commands[command] then
local args = {}
for i = 2, #words do
table.insert(args, words[i])
end
commands[command](unpack(args))
end