The code below is a basic Lua text extractor and comparing algorithm. It uses tables and for loops to cycle through all words a GUI object may contain, then compare them to the input you’ve typed in the TextBox.
The search functionality relies on a GUI (Graphical User Interface) system, which is included in the open-source game. The pages, blocks, text box elements and the script are part of a GUI layout in the game.
How does it work? Step-by-step
-
The script sets up a list called
IgnoredWords
, which contains various common English words that will be ignored during the search process. These words are often called “stop words” as they don’t carry significant meaning and are typically excluded from text analysis. (Lines 4-42) -
The
Search
function is defined, which is the main search logic for this script. It is triggered when theFocusLost
event of thetextBox
occurs, indicating that the user has entered some text and moved the focus away from the text box. (Lines 45 & 107) -
If the player enters 2 or less lone words/numbers, then the script doesn’t continue. This is optional and can be removed without the script breaking. (Lines 2 & 47)
-
Inside the
Search
function, the entered text from thetextBox
is extracted, converted to lowercase, and split into individual words, which are stored in theKeywords
table. (Line 48) -
The script iterates over all the pages (blocks of text) located under the
Pages
object. For each page, it retrieves all descendant objects that are eitherTextLabel
orTextButton
and extracts the text content from them. The text content is then converted to lowercase and split into individual words, which are stored in theComparisonTable
. (Lines 56-71) -
The script then proceeds to remove any stop words (words listed in
IgnoredWords
) from theComparisonTable
. (Lines 73-79) -
It calculates the number of matches between the keywords entered by the user (
Keywords
) and the words found in the page’s text (ComparisonTable
). Each occurrence of a matching word increments theMatches
counter. (Lines 81-87) -
Depending on the number of matches (
Matches
), the script determines whether the page’s correspondingBlock
should be visible or hidden. TheBlock
appears to be a graphical element associated with the page, and its visibility and layout order are adjusted based on the number of matches. (Lines 89-95) -
The script outputs information about the search results and the process via
warn
andprint
statements, providing details about the page name, block heading, the number of matches found, and the words found on the page. This is optional and can be removed without the script breaking. (Lines 54, 55, 96-100)
Script
If you arent familiar with advanced lua coding, I suggest you thoroughly read the Step-by-step description above.
textBox = script.Parent.TextBox
local MinWordsToSearch = 3
local IgnoredWords = {
"this",
"for",
"neither",
"and",
"not",
"nor",
"whether",
"but",
"so",
"or",
"such",
"yet",
"as",
"also",
"while",
"still",
"until",
"too",
"unless",
"only",
"since",
"however",
"which",
"otherwise",
"where",
"who",
"than",
"after",
"because",
"either",
"whoever",
"nevertheless",
"though",
"else",
"although",
"if",
"till"
}
local function Search(enterPressed, _inputObject)
if enterPressed then
if textBox.Text:gsub("%p", ""):len() > MinWordsToSearch - 1 then
local Keywords = textBox.Text:lower():gsub("%p", ""):split(" ")
for key, value in pairs(Keywords) do
if not value:match("%S+") then
Keywords[key] = nil
end
end
warn("Search Report")
print("Searched keywords: ", Keywords)
for i, page in pairs(script.Parent.Pages:GetChildren()) do
local ComparisonTable = {}
local Matches = 0
for i, v in pairs(page:GetDescendants()) do
if v:IsA("TextLabel") or v:IsA("TextButton") then
local words = v.Text:lower():gsub("%p", ""):split(" ")
for key, value in pairs(words) do
if not value:match("%S+") then
words[key] = nil
end
end
for index, TableChild in pairs(words) do
table.insert(ComparisonTable, words[index])
end
end
end
for ind, obj in pairs(ComparisonTable) do
for ind2, obj2 in pairs(IgnoredWords) do
if obj == obj2 then
table.remove(ComparisonTable, ind)
end
end
end
for I, V in pairs(ComparisonTable)do
for II, VV in pairs(Keywords) do
if V == VV then
Matches += 1
end
end
end
local Block = page.BlockRefference.Value
if Matches > 0 then
Block.Visible = true
Block.LayoutOrder = Matches*-1
else
Block.Visible = false
end
warn("Page name: " .. page.Name)
warn("Block heading: " .. Block.Redirect.Text)
warn("№ of matches found: " .. Matches)
print("Words found on page: ", ComparisonTable)
warn("———————————————————————————————————————————")
end
end
end
end
textBox.FocusLost:Connect(Search)
Useful pages if you wish to modify the script for your needs:
If you have any feedback feel free to reply, it really helps!
- Yes
- No
- Maybe someday
0 voters
This search engine will be improved and used in my upcoming games, mostly in the one below:
FOS Public Testing Release V2 - Roblox
It will also be included in the upcoming sequel of Libervian Republic, a real-world simulation.
If you wish to help and become apart of the Libervian Development Team feel free to write me a message, I’m available on TalentHub or generally Roblox itself.