Creating a player search GUI using ScrollingFrame

I’m trying to create a player records search GUI, and I’m using a ScrollingFrame coupled with a script that creates a new TextLabel for each name in the dropdown.

This is the code behind it:

function makeButton(player)
	scrollingFrame.Visible = true
	local newButton = Instance.new("TextButton",scrollingFrame)
		newButton.Size = UDim2.new(0, 175 ,0, 25)
		newButton.BackgroundColor3 = Color3.fromRGB(56, 142, 60)
		newButton.TextColor3 = Color3.fromRGB(255,255,255)
		newButton.BackgroundTransparency = 0
		newButton.BorderSizePixel = 0
		newButton.Font = "SourceSans"
		newButton.Text = player.Name
		newButton.TextSize = 18
		newButton.ZIndex = 4
		newButton.Name = "LookupButton"
	local newScript = script.Parent.PlayerClickCopy:Clone()
		newScript.Parent = newButton
		newScript.Disabled = false
end

function makeButtonsFromPlayers(players,length,lookUpString)
	if string.lower(string.sub(players.Name,1,length)) == string.lower(lookUpString) then
		if players.name == lookUpString then
			scrollingFrame.Visible = false
		else
			makeButton(players)
		end
		
	end
end

textBox:GetPropertyChangedSignal("Text"):connect(function()
	removeOldButtons()
	local lookUpString = textBox.Text
	local children = game.Players:GetPlayers()
	local length = string.len(lookUpString)
	for _,players in pairs(children) do
		if textBox.Text == "" then
			scrollingFrame.Visible = false
			break
		else
			makeButtonsFromPlayers(players,length,lookUpString)
		end
	end
end)

When I type in “A”, and there’s more than one person whose name begins with “A” in game, it creates two labels, but doesn’t set Position properly. I can’t figure out how I should increment Position to make it show labels separately.

My aim here is advice on how to set position for each label so that as I scroll, each name is displayed separately, not all on each other.

5 Likes

In this case the easiest way would be to insert a UIListLayout inside the ScrollingFrame. This will do the job of laying out the buttons for you in order, inside the frame. When you want to show buttons, Parent them to the ScrollingFrame, otherwise delete/reparent elsewhere temporarily.

6 Likes

So I put a UIListLayout in the ScrollingFrame and set its properties to what I wanted. Still, when I use the code above, I can’t get them to appear in a list.

I was setting each button’s name to the same thing :100:

Hmmmm can you give me the Removeoldbuttons() function, I need it for my game. :face_with_monocle: