Unreliable Module Search?

So, I made a local script dedicated to monitoring text box input, before searching a module full of commands to check if there is a match for it or not.

Code:

local AllCommands = require(game.ReplicatedStorage.commands)
script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	for index,Command in pairs(AllCommands) do
		if string.match(string.lower(Command), "^"..string.lower(script.Parent.Text)) then 
			script.Parent.Parent.Matched.TextColor3 = Color3.new(0,1,0)
			script.Parent.Parent.Matched.Text = "Closest Match: "..Command		
		else
			script.Parent.Parent.Matched.TextColor3 = Color3.new(1, 0, 0)
			script.Parent.Parent.Matched.Text = "Unknown Command"
		end
	end
end)

But for some reaon, the text is changed to unknow command every time I type something that is in the module, despite me typing them in properly. It’s also weird as it seems to detect and set the match to warn when I type in something similar to it, but nothing else. Help?

Edit
I forgot to include the module, so here it is (It’s fairly simple):

local module = {
	"Kick",
	"Kill",
	"Fling",
	"Ban",
	"Speed",
	"Jump",
	"Warn"
}

return module

break out of the loop after you find the first match:

			script.Parent.Parent.Matched.Text = "Closest Match: "..Command		
			break

Or better, do the loop once and change the text once:

local found = -1

for index,Command in pairs(AllCommands) do
	if string.match(string.lower(Command), "^"..string.lower(script.Parent.Text)) then 
		found = index
		break
	end
end

if found > 0 then 
	script.Parent.Parent.Matched.TextColor3 = Color3.new(0,1,0)
	script.Parent.Parent.Matched.Text = "Closest Match: "..AllCommands[index]		
else
	script.Parent.Parent.Matched.TextColor3 = Color3.new(1, 0, 0)
	script.Parent.Parent.Matched.Text = "Unknown Command"
end
1 Like