Search system for my title textbuttons

Hey,

I tried to find help for my issue, but those solutions were not helpful for me. I want a search system that searches for the items the player enters into the search bar (textbox) and then arranges them in the correct order, hiding the others that do not match the results. For example, if you enter ‘Crea,’ it searches for all the titles that start with ‘Crea’ and hides the others that do not start with ‘Crea.’ The code contains all the locations and titles that I have.

They are not searched by the names of those TextButtons but by the texts on the TextButtons, i.e., TextButton.Text. Can you help me with this? Here is my code outline that currently hides everything and does not display anything, no matter what I enter:

local search = script.Parent
local title1 = script.Parent.Parent.hiiret.e1
local title2 = script.Parent.Parent.hiiret.e2
local title3 = script.Parent.Parent.hiiret.r1
local title4 = script.Parent.Parent.hiiret.r2
local title5 = script.Parent.Parent.hiiret.r3
local title6 = script.Parent.Parent.hiiret.r4
local title7 = script.Parent.Parent.hiiret.s1
local title8 = script.Parent.Parent.hiiret.s2
local title9 = script.Parent.Parent.hiiret.l1
local title10 = script.Parent.Parent.hiiret.l2
local title11 = script.Parent.Parent.hiiret.m
local title12 = script.Parent.Parent.hiiret.vip

local titlepos1 = title1.Position
local titlepos2 = title2.Position
local titlepos3 = title3.Position
local titlepos4 = title4.Position
local titlepos5 = title5.Position
local titlepos6 = title6.Position
local titlepos7 = title7.Position
local titlepos8 = title8.Position
local titlepos9 = title9.Position
local titlepos10 = title10.Position
local titlepos11 = title11.Position
local titlepos12 = title12.Position

local hiiret = script.Parent.Parent.hiiret
local search = script.Parent
local originalPositions = {
	Squirrel = hiiret.r1.Position,
	Coder = hiiret.r2.Position,
	Sauna = hiiret.r3.Position,
	QWERTY = hiiret.r4.Position,
	Nut = hiiret.s1.Position,
	Helper = hiiret.s2.Position,
	Epic = hiiret.e1.Position,
	Creative = hiiret.e2.Position,
	Master = hiiret.l1.Position,
	M3CHA = hiiret.l2.Position,
	PlayerChosen = hiiret.m.Position,
	VIP = hiiret.vip.Position,
}

local function updatePositions(query)
	query = query:lower()

	local visibleTitles = {}
	local yOffset = 0


	for name, position in pairs(originalPositions) do
		local title = hiiret:FindFirstChild(name)

	
		if title and title:IsA("TextButton") then
			local titleText = name:lower()
			if titleText:find(query, 1, true) then
				table.insert(visibleTitles, title)
				title.Position = UDim2.new(position.X.Scale, position.X.Offset, position.Y.Scale, yOffset)
				yOffset = yOffset + position.Y.Offset
			else
				title.Position = UDim2.new(position.X.Scale, position.X.Offset, 2, 0)
			end
		end
	end


	for _, title in pairs(hiiret:GetChildren()) do
		if title:IsA("TextButton") and not table.find(visibleTitles, title) then
			title.Position = UDim2.new(0, 0, 2, 0)
		end
	end
end


search:GetPropertyChangedSignal("Text"):Connect(function()
	updatePositions(search.Text)
end)


This is how my buttons are arranged within the frame. The red one is the button where you can enter your own text, which complicates this search system. Because it can be edited, it is textbox:

Hey, Could you explain what do you want do because i really couldnt understand. But if you wanna search for each textbox and their text you could use loops. Ill give an example

--Script located in a place with the textboxes
local ChildrenToGet = Script.Parent:GetChildren()
for Number, Instance in pairs(ChildrenToGet) do
  Instance.Text = "This is tile "..Number
-- Yes you can change it to whatever
end

Hey,

This game’s system is best to describe system that I want:
This is from Pet simulator X

These are searched based on the text of these TextButtons (and TextBox), so if the text of one TextButton is ‘Feed me!’ and the player enters ‘Fee’ in the search TextBox, it will display this title because it starts with ‘Fee’. So these are searched based on the text of these TextButtons (and TextBox) and not these TextButtons’ (and TextBox’s) names.

A similar system, but instead of pets, we have titles, and these titles are TextButtons, except for 1 which is a TextBox. The order in the Frame, where all these titles are, is the same as in the previously provided image. Do you understand now?

Oh i get it now ill make you a script

--Script located in a place with the textboxes
local stringtofind = "NAME" -- the string you want to find in textbox text
local ChildrenToGet = script.Parent:GetChildren() -- the place of the textboxes/textlabels

function CheckWhatPlayerWrote()
	for Number, Value in pairs(ChildrenToGet) do
		--Value means instance
		-- change the "textlabel" value below if the title1/2/3/4/5/6/7/8/9/10/11/12 are frames, textlabels, textboxes
		if Value:IsA("TextBox") then
			if Value.Text:find(stringtofind) then
				-- this will apply to all the textboxes that matches what the player wrote
				local AllWhichMatched = Value
				-- type your function here
				-- example os
				AllWhichMatched.Text = "Found!"
				-- or
				AllWhichMatched.Visible = true
				-- If you want the one which didnt match type in the else section
			else
				-- this is the else section
				local AllWhichMatched = Value
				AllWhichMatched.Visible = false
			end
		end
	end
end

If you have any questions/problems, Feel free to ask me!
Sorry! for responding late too.

  1. Make all the thiings to match the same case (just say lower) and put all the strings in an array.
  2. Make a scrolling frame with a UIListLayOut or something to hold the resulting buttons.
    for example, you could do
local Texts = {}
for _, a in in pairs(WhereYourTBsAre:GetChildren()) -- or use collection service, whatever
   Texts[a] =     a.Text:lower()
end 

local last = TextBox.Text 

TextBox.FocusLost:Connect(function()
  local now = TextBox.Text:lower()
  if (not now) or (#now == 0) then   --  Unless you want the emptry string to match everything 
    TextBox.Text = last     
    return 
 end 
  last = now 
  for tb, text in pairs(Texts) do 
    if text:find(now) then  --  This will match subwords .. for example, ha will match shame ... maybe you don't want this? 
       tb.Parent = FrameWithUiListLayout
    else 
      tb.Parent = script --  Just keeps the button from being seen  
    end 
  end 
end 

Hey,

Thanks for help! That lower helped me. Now the next problem is this:

The positions are not working as I would like. They simply disappear and reappear, but do not arrange themselves neatly. All those that are visible should always be in a tidy order and not have gaps, as seen in the video. They should sort of condense whenever the adjacent one disappears. Do you understand? The positions of all the elements are in the code below, and this is my current code:

local search = script.Parent
local title1 = script.Parent.Parent.hiiret.e1
local title2 = script.Parent.Parent.hiiret.e2
local title3 = script.Parent.Parent.hiiret.r1
local title4 = script.Parent.Parent.hiiret.r2
local title5 = script.Parent.Parent.hiiret.r3
local title6 = script.Parent.Parent.hiiret.r4
local title7 = script.Parent.Parent.hiiret.s1
local title8 = script.Parent.Parent.hiiret.s2
local title9 = script.Parent.Parent.hiiret.l1
local title10 = script.Parent.Parent.hiiret.l2
local title11 = script.Parent.Parent.hiiret.m
local title12 = script.Parent.Parent.hiiret.vip

local titlepos1 = title1.Position
local titlepos2 = title2.Position
local titlepos3 = title3.Position
local titlepos4 = title4.Position
local titlepos5 = title5.Position
local titlepos6 = title6.Position
local titlepos7 = title7.Position
local titlepos8 = title8.Position
local titlepos9 = title9.Position
local titlepos10 = title10.Position
local titlepos11 = title11.Position
local titlepos12 = title12.Position

local titles = script.Parent.Parent.hiiret



local function updatePositions()
	local text = search.Text:lower()
	if text ~= "" then -- if it has text
		local buttons = titles:GetDescendants() -- all of the buttons
		for _, button in pairs(buttons) do -- loops through the buttons
			if button:IsA("TextButton") or button:IsA("TextBox") then -- if it's a button
				local buttonText = button.Text:lower() -- lowercase button text
				if string.find(buttonText, text) then -- if search bar text is found in the button's text
					button.Visible = true -- shows button
				else -- otherwise
					button.Visible = false -- hides button
				end
			end
		end
	else -- if it's empty
		local buttons = titles:GetDescendants() -- all buttons
		for _, button in pairs(buttons) do -- loops through buttons
			if button:IsA("TextButton") or button:IsA("TextBox") then -- if it's a button
				button.Visible = true -- shows button
			end
		end
	end
end


search:GetPropertyChangedSignal("Text"):Connect(function()
	updatePositions()
end)


Use a UIGridLayout. When made invisible everything will fill in the gaps. It will also save you a lot of time with placing things as things are placed automatically.

I also have thinked about that UIGridLayout but I don’t know how should I do it because I haven’t used any UIGridLayouts and I have no idea how should I use it. I only know what it do.

Well, now is a great time to try it. It does everything for you. It also is the only thing that can really solve your problem in a reasonable way.

now I have it! But I don’t know how to fix this:
problem

It should look like this:
real

Add padding via UIPadding (?) or the property in the UIGridLayout

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.