Search Bar - Not working

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

searchbar.Changed:Connect(UpdateResults)

2 Likes
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!)

I’m not sure how to format it like that. But thanks for that advice.

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.


Also for formating you just do this

put 3 ``` before and after your code

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)
1 Like

Its actually much better if you explained what was wrong in the code, so others can learn too :slightly_smiling_face:

1 Like

The person’s issue had to do with string.find, You need a numerical value for it to find it.

string.match attempts to match the value.

Let’s say string1 == string2. To find if this is true, we need to do

if string.match(string1, string2), then print("works"); else print("error") end

I’ve fixed it now, thanks everyone for you’re help.

1 Like

It’s a bit complicated.

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)

1 Like