How do I make a search bar TextBox?

I’m working on a phone GUI right now, and I need to know how to make a search bar, that lets you search for players in the playerlist, so you can call them. I need this because the servers of the game could reach 40 players, making it difficult to find the person you want to call. I’ve tried to do this, but I’m struggling with an error, saying "Current Parent of [Textbutton’s name here] is locked, current parent, NULL, new parent Folder. Here is some of the code for it right now:

--  callerElementsFolder is a new folder parented to nil.
function search(text)
	local text = string.lower(text)
	local callerListEls = callerList:GetChildren()
	local passTable = {}
	local callerElementsParts = callerElementsFolder:GetChildren()
	
	if searched == true then
		for i, v in pairs(callerListEls) do
			v:Destroy()
		end
		
		for i, v in pairs(callerElementsParts) do
			v.Parent = callerList
		end
	end
	
	for i, v in pairs(callerListEls) do
		v.Parent = callerElementsFolder
	end
	
	for i, v in pairs(callerListEls) do
		if string.find(string.lower(v.Text), text) then
			table.insert(passTable, v:Clone())
		end
	end
	
	for i, v in pairs(passTable) do
		v.OrderPos.Value = i
		v.Position = UDim2.fromScale(v.Position.X.Scale, v.OrderPos.Value*offsetStandard)
		v.Parent = callerList
	end
	
	searched = true
end

searchBar:GetPropertyChangedSignal("Text"):Connect(function()
	local text = searchBar.Text
	
	search(text)
end)

Any method suggestions are appreciated.

1 Like

Edit: I’ve changed the code, and it works much better now. I had to wrap something in a pcall, because it was erroring, yet working as intended. Here’s the code, but there’s still a problem shown in the video:

function search(text)
	local text = string.lower(text)
	local callerListEls = callerList:GetChildren()
	local passTable = {}
	local callerElementsParts = callerElementsFolder:GetChildren()
	
	if searched == true then
		for i, v in pairs(clonedTable) do
			pcall(function()
				v:Destroy()	
			end)
		end
		
		for i, v in pairs(callerElementsParts) do
			v.Parent = callerList
		end
	end
	
	wait(0.01)
	
	for i, v in pairs(callerListEls) do
		pcall(function()
			v.Parent = callerElementsFolder
		end)
	end
	
	for i, v in pairs(callerListEls) do
		if string.find(string.lower(v.Text), text) then
			local new = v:Clone()
			table.insert(passTable, new)
			table.insert(clonedTable, new)
		end
	end
	
	for i, v in pairs(passTable) do
		if v:FindFirstChild("OrderPos") then
			v.OrderPos.Value = i
			v.Position = UDim2.fromScale(v.Position.X.Scale, (v.OrderPos.Value*offsetStandard) - offsetStandard)
		end
		v.Parent = callerList
	end
	
	searched = true
end

searchBar:GetPropertyChangedSignal("Text"):Connect(function()
	local text = searchBar.Text
	
	search(text)
end)

Nevermind, fixed it, I arrived at some new code:

function search(text)
	local text = string.lower(text)
	local passTable = {}
	local callerElementsParts = callerElementsFolder:GetChildren()
	
	if searched == true then
		for i, v in pairs(clonedTable) do
			v:Destroy()
		end
		
		for i, v in pairs(callerElementsFolder:GetChildren()) do
			v.Parent = callerList
		end
	end
	
	wait(0.01)
	local callerListEls = callerList:GetChildren()
	
	for i, v in pairs(callerListEls) do
		v.Parent = callerElementsFolder
	end
	
	for i, v in pairs(callerListEls) do
		if string.find(string.lower(v.Text), text) then
			local new = v:Clone()
			table.insert(passTable, new)
			table.insert(clonedTable, new)
		end
	end
	
	for i, v in pairs(passTable) do
		if v:FindFirstChild("OrderPos") then
			v.OrderPos.Value = i
			v.Position = UDim2.fromScale(v.Position.X.Scale, (v.OrderPos.Value*offsetStandard) - offsetStandard)
		end
		v.Parent = callerList
	end
	
	searched = true
end

searchBar:GetPropertyChangedSignal("Text"):Connect(function()
	local text = searchBar.Text
	
	search(text)
end)
3 Likes