I’m trying to make a search bar for my inventory system.
The search bar works but after I’ve stopped typing the items that aren’t being searched up show up again. Then when you type another letter the searched up item appears again then disappears again after a second. I’ve tried fixing it to no use.
local searchBar = script.Parent.Search
local items = script.Parent.Items
searchBar:GetPropertyChangedSignal("Text"):Connect(function()
local function match_strings(string1, string2)
local matches = 0
for i = 1, #string1, 1 do
if string.sub(string1, i, i) == string.sub(string2, i, i) then
matches += 1
end
end
return matches == #string1
end
local function SortBySearch()
for i, item in ipairs(items:GetChildren()) do
if item:IsA("ImageButton") then
if searchBar.Text == "" then
item.Visible = true
continue
end
local search_text = string.lower(searchBar.Text)
local item_name = string.lower(item.Name)
match_strings(search_text, item_name)
end
end
end
SortBySearch()
end)
I think your problem is this
here is the modified code which i think is your mistake
local searchBar = script.Parent.Search
local items = script.Parent.Items
searchBar:GetPropertyChangedSignal("Text"):Connect(function()
local function match_strings(string1, string2)
local matches = 0
for i = 1, #string1 do
if string.sub(string1, i, i) == string.sub(string2, i, i) then
matches = matches + 1
end
end
return matches == #string1
end
local function SortBySearch()
local search_text = string.lower(searchBar.Text)
for i, item in ipairs(items:GetChildren()) do
if item:IsA("ImageButton") then
local item_name = string.lower(item.Name)
local isMatch = match_strings(search_text, item_name)
item.Visible = (search_text == "" or isMatch)
end
end
end
SortBySearch()
end)
In this fixed version:
I moved the declaration of the search_text variable outside of the for loop. It is not necessary to declare it at every iteration.
Inside the for loop, we call the match_strings function and store its result in the isMatch variable.
Then, use the expression (search_text == “” or isMatch) to set the Visible property of the item element. If search_text is empty or if there is a match, the element will be visible; otherwise it will be invisible.
I hope I’ve helped
The same problem still occurs when I search after around one second of not typing all the items become visible again.
What I want it do is keep the item that has appeared in the search visible and not let the other items become visible. I tried looping the function sort by search it fixed the problem but searching was extremely fluttery and did not work very well.
My apologies for the earlier confusion. I misunderstood. To keep the searched element visible and hide the others you can modify the code as follows:
local searchBar = script.Parent.Search
local items = script.Parent.Items
searchBar:GetPropertyChangedSignal("Text"):Connect(function()
local search_text = string.lower(searchBar.Text)
for i, item in ipairs(items:GetChildren()) do
if item:IsA("ImageButton") then
local item_name = string.lower(item.Name)
local isMatch = string.find(item_name, search_text, 1, true)
item.Visible = (isMatch ~= nil)
end
end
end)
I think you mean this
if a problem occurs tell me to see how I can fix it.
In my inventory script I’m constantly removing and updating the items because the script is reading the tools and deleting all the items and updating them so I don’t know if a search system can work.
Do you want to modify the code to store the original visibility state of each element before the search, and then restore it after updating the elements?
local searchBar = script.Parent.Search
local items = script.Parent.Items
-- Stores the original visibility state of each element
local estadoVisibilidad = {}
for i, item in ipairs(items:GetChildren()) do
if item:IsA("ImageButton") then
estadoVisibilidad[item] = item.Visible
end
end
searchBar:GetPropertyChangedSignal("Text"):Connect(function()
local textoBusqueda = string.lower(searchBar.Text)
for i, item in ipairs(items:GetChildren()) do
if item:IsA("ImageButton") then
local nombreItem = string.lower(item.Name)
local coincide = coincideCadenas(textoBusqueda, nombreItem)
-- Set visibility based on search result
item.Visible = (textoBusqueda == "" or coincide) and estadoVisibilidad[item]
end
end
end)
local function coincideCadenas(cadena1, cadena2)
local coincidencias = 0
for i = 1, #cadena1, 1 do
if string.sub(cadena1, i, i) == string.sub(cadena2, i, i) then
coincidencias = coincidencias + 1
end
end
return coincidencias == #cadena1
end
In this way, the search system will preserve the original visibility state of each element and restore it after the update.