A creative way to use table.find?

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.

Well, the first parameter is correct, I don’t know how the table is supposed to know this information

Cause it’s comparing what’s found in the table which is “kill” with string.find(textthere, “kill”)

1 Like

I don’t know how the table is supposed to know this information

So “texthere” is assigned when the players focus is off the TextBox using “FocusLost”. So I’m asking it to search that string for “kill”.

Cause it’s comparing what’s found in the table which is “kill” with string.find(textthere, “kill”)

This is isn’t really important atm, because it states that the command isn’t found. Which is why I think that I’m using table.find incorrectly.

I don’t think what you’re saying is comparable… one’s a string value, another is a boolean. They act very differently.

Yes, you are using it wrong

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

1 Like

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.

I see what you’re saying now, after reviewing it makes more sense. Thanks a ton.

string.find returns where the first match starts and ends, not a boolean. @BiIinear

local started, ended = string.find("some text", "text")
print(started, ended) -- 6 9

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
1 Like