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.
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
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
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
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.