Playerlist autofill

I have scripted a TextBox where it will autofill (adding the players name to a table) by identifying the first few characters of the players name (as seen below)

local players = {"josh", "josking", "joling", "parny", "paxton"} -- mock table of players
local players2 = {}

for i, v in pairs(players) do
		if string.lower(v):sub(1, #EnterPlayerName.Text) == EnterPlayerName.Text then
			table.insert(players2, v)
		end
	end

What I currently was it so that it displays the list of people in the table, which I tried to do;

for i, v in pairs(players2) do
		PlayerNameFrame[i].Visible = true -- playernameframe[i] references the button which is a number
		PlayerNameFrame[i].Text = v
	end

But this isn’t working and currently it displays all the players names whenever I type the corresponding letters
image_2022-08-01_034223357

Any help on how to fix this problem would be great, thanks

You should try checking what is inside the textbox each time you type/delete a letter

1 Like

Are you emptying the players2 table every time a player enters text? I am unable to reproduce the issue with the snippets of code you have provided. Can you please send the rest of the code.

local ShowUI = game.ReplicatedStorage.OrderEvents.ShowUI

local MainFrame = script.Parent.Background

local PlayerNameFrame = MainFrame.PlayerName
local EnterPlayerName = PlayerNameFrame["Enter Player Name"]
local PlayerButtonNames = {"1", "2", "3", "4", "5", "6"}

local SelectedPlayer

ShowUI.OnClientEvent:Connect(function()
	PlayerNameFrame.Visible = true
end)

local players = {"josh", "josking", "joling", "parny", "paxton"}
local players2 = {}

EnterPlayerName:GetPropertyChangedSignal("Text"):Connect(function()	
	players2 = {}
	
	for i, v in pairs(players) do
		if string.lower(v):sub(1, #EnterPlayerName.Text) == EnterPlayerName.Text then
			table.insert(players2, v)
		end
	end
	
	for i, v in pairs(players2) do
		PlayerNameFrame[i].Visible = true
		PlayerNameFrame[i].Text = v
	end
	
	if EnterPlayerName.Text == "" then
		for _,Buttons in pairs(PlayerNameFrame:GetChildren()) do
			for _,ButtonNames in pairs(PlayerButtonNames) do
				if Buttons.Name == ButtonNames then
					Buttons.Visible = false
					Buttons.Text = ""
				end
			end
		end
	end
end)

I think the issue is that it when I typed “j” it added all 3 j’s to the table - and so it doesnt matter what you type next as all 3 are already there and so it’s being displayed.

It doesn’t appear you’re setting the other buttons to be invisible if they’re not found in the players2 table thus they’ll always be visible no matter the results.

I see, how am I able I fix this?

The easiest fix for you would be to loop through PlayerNameFrame and just set them all to be invisible before looping through players2.

1 Like

Above this line you could set all of the button frames to be invisible. or you could just clone them instead of making them visible. You can even just check if they are in the table and if not in table then make it invisible. Also, I could be wrong but you should use ipairs

1 Like