Hello! I’m creating a Search Bar and I have got a slight issue. There is no errors but the script does not work.
Code:
local searchbar = script.Parent.SearchBar
local items = script.Parent.Items
function UpdateResults()
local search = string.lower(searchbar.Text)
for i, v in pairs(items:GetChildren()) do
if v:IsA(“GuiButton”) then
if search == “” then
local item = string.lower(v.Text)
if string.find(item, search) then
v.Visible = true
else
v.Visible = false
end
else
v.Visible = true
end
end
end
end
local searchbar = script.Parent.SearchBar
local items = script.Parent.Items
function UpdateResults()
local search = string.lower(searchbar.Text)
for i, v in pairs(items:GetChildren()) do
if v:IsA(“GuiButton”) then
if search == “” then
local item = string.lower(v.Text)
if string.find(item, search) then
v.Visible = true
else
v.Visible = false
end
else
v.Visible = true
end
end
end
end
searchbar.Changed:Connect(UpdateResults)
(This is not a solution, it is just to read the code easier, @tyler09456 please format code properly next time so others can read it, thanks!)
Could you please tell me why is there a condition statement inside the loop that is this one:
if search == “” then
I believe you want to do just make everything visible if its empty? Then you will actually wanna make it to
if not search == "" then
so it makes the item visible if search is not having anything, as I can see everything else seems to be working. If not I would recommend placing prints for basic debugging and checking till where the code runs.
local TextBox = script.Parent.SearchBar
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local items = script.Parent.Items
for _,v in pairs(items:GetChildren()) do
local lower = string.lower(TextBox.Text)
local loweritem = string.lower(v.Name)
if string.match(loweritem,lower) then
if script.Parent.Text == "" or script.Parent.Text == " " then
-- means that theres no text inside the box
else
-- the text matchs the item name-
end
end
end
end)
I Used :GetPropertyChangedSignal because .Changed did not work out for me pretty well.
And I think they wanted to check if the search was empty, but I’m not sure because they didin’t gave enough details about what they wanted to accomplish.
And they used string.find to check if the lower text was same as the lower item.
But string.match works better than from string.find. (Because I think they needed a number value for string.find)