How would i make a "autocorrect" feature for my game?

im makign a game where you can type in commands and stuff, and i want a “autocorrect” thing

For example:

User types “/Loa”
Recomendations:
/LoadImg
/LoadDisc1
/LoadDisc2

I would image the way i would do this is get a table of all the commands, then find the best mathing ones, but im newer to scripting, so idk how to do advanced stuff with strings.

Thanks!

You can use string.find

String.find will be your friend. With this, you can see if a match is contains anywhere within the string. If it returns nil, then it is not a match. Here is an example:

local TestString = "Testing This" 

if string.find(TestString, "Test")  ~= nil then 
    print("Found") 
else 
    print("Not found!")
end

i was thinking of using that, although it looked complicated, how would i do this with maybe a table of strings?

1 Like

This isn’t too hard to do. By doing the above, we can almost use the exact same code, and just loop each string with a for loop. Here is an example function, that returns a table with the next strings that fit the pattern


local TableOfStrings = {"String1", "String2", "This should not go through!"} 

local function ReturnNewTableOfFinds(SearchTable, TheStringPattern) 
	local MakeTableOfFinds = {}
	
	for Index, TheString in SearchTable do 
		if string.find(TheString, TheStringPattern) ~= nil then 
			table.insert(MakeTableOfFinds, TheString)
		end
	end
	
	return MakeTableOfFinds
end 

local TheFinds = ReturnNewTableOfFinds(TableOfStrings, "String")

--A Test print code, to print in the output to show it works! 
for Index, TheString in TheFinds do 
	print(Index..TheString)
end

ill try to integrate that, thanks!
(ill mark as solution if it works)

Tried to implement it, didnt work. Am i doing somethin wong?

local function ReturnNewTableOfFinds(SearchTable, TheStringPattern) 
	local MakeTableOfFinds = {}

	for Index, TheString in SearchTable do 
		if string.find(TheString, TheStringPattern) ~= nil then 
			table.insert(MakeTableOfFinds, TheString)
		end
	end

	return MakeTableOfFinds
end 


script.Parent.TextBox:GetPropertyChangedSignal("Text"):Connect(function()

	local TableOfCommands = {"/help", "/loadimg", "/loadsong", "/viewport", "/fps"} 

	local TheFinds = ReturnNewTableOfFinds(TableOfCommands, "String")

	for Index, TheString in TheFinds do 
		print(Index..TheString)
	end
end)

Yes. This line right here. “String” is the search phrase. In this case, this needs to be changed to what the user typed. This is the “/Loa” in the example you had

Kinda works, but there is a number infront of each result

That is what “Index” does in the print in the below line. I added this print to show the position it is in the list. Does not impact the code functionality and you can just remove “Index…” if you don’t want it to show the position it is in.

Thanks! i also made the suggestion UI, it works really well!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.