Printing 'Error' when it shouldn't be

So I’m making some commands, and I have a table with like 3 commands and each of them have a Name inside of them, so my script checks if the Name is the same as the text of TextBox. When it’s not, it prints Error. After I put the right commands in, it still prints error

Heres the script

local cmds = {
	Cmd3 = {
		main = function()
			if (currentsplit[1] == 'exit') then
				for _, v in next, currentOpen do
					v.Visible = false
				end
			end
		end,
		namef = 'exit'
	}
}
cmdbar.FocusLost:Connect(function(enter)
	local text = cmdbar.Text
	local split = string.split(text, ' ')
	
	currentsplit = split
	
	if (enter) and (text ~= nil) and (currentsplit ~= nil) then
		for numbi, func in next, cmds do
			if (func ~= nil) then
				if (func.main) then
					func.main()
				end
			end
		end
		
		-- error handler
		-- here
		for i, v in next, cmds do
			if (text ~= v.namef) then
				print('error')
			end
		end
	end
end)

I can’t put the whole script because its 80 lines.

Also, the function will run even if it gets an error.

Capture

Put a print before the error statement:

		-- error handler
		-- here
		for i, v in next, cmds do
            print(text, v.namef)
			if (text ~= v.namef) then
				print('error')
			end
		end

Then send the results here

So I also have other commands, like 2 more I didnt put them so the script isnt like 100 lines anyways heres the results

  helloworld about  
 helloworld helloworld
   helloworld exit

If you have 3 commands and one of them is the one you put in your command bar, it should still print “error” 2 times because their name don’t match

The best approach would probably be to remake your commands’ “main” functions and do the currentsplit[1] check before you call the said functions, as well as add a return statement after you call your function like so:

local cmds = {
	Cmd1 = {
		main = function()
			print'cmd1 ran'
		end,
		namef = 'exit1'
	},
	Cmd2 = {
		main = function()
			print'cmd2 ran'
		end,
		namef = 'exit2'
	},
	Cmd3 = {
		main = function()
			print'cmd3 ran'
		end,
		namef = 'exit3'
	}
}

cmdbar.FocusLost:Connect(function(enter)
	local text = cmdbar.Text -- don't think this should ever be nil, but rather an empty string ""
	local split = string.split(text, ' ')
	local cmd = table.remove(split, 1) -- this is "currentsplit" from your code
	
	if (enter) and (text ~= '') then
		for numbi, func in next, cmds do
			if (func ~= nil) then
				if (func.main and func.namef == cmd) then
					func.main()
					-- exit function with return
					return
				end
			end
		end
		
		-- if no functions were matched to the text in cmd bar
		print('error')
	end
end)

Putting “exit2” in the cmd bar should make it print “cmd2 ran”, while running a non-existent command should print “error”