Auto-Completing a player's name from TextBox input

Hello! I am here to ask an age-old question, how do I auto-complete a name in a textbox?

I thought I had formed a small base, just to find someone’s name, however even this isn’t working!

Here is the code that I attempted:

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local text = string.match(script.Parent.Text, game.Players.LocalPlayer.Name)
	local text1 = tostring(text)
	print(text1)
	script.Parent.Text = (text1)
end)

Thanks for your help!

1 Like

Capture the text and see if it is currently matching with the first 3-5 characters of the player’s name, if it is, replace the text entirely with the player’s name.

You may as well as use string:lower() to be friendly towards case sensitive characters in their username or the text it self.

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	if (game.Players.LocalPlayer.Name:lower():sub(1, 4) == text:lower():sub(1, 4) then
        script.Parent.Text = game.Players.LocalPlayer.Name
    end
end)
``
2 Likes

Hello! I’ve found a thread that had this same exact question, It has a valid solution so it might help you too!

This: How can I create a TextBox where the username will get autocompletet?

1 Like

That works, but how would I get it to work for any player in the game?

Would I create a table of players and match it from there?

I really struggled to understand that, I’ve scoured the forum for hours looking for something like this and to no avail.

It will work for everyone in the game assuming that you are doing this on the client side which is what you’re doing.

So, say I wanted to search someone’s name up, like lets say SilentsReplacement and my name is KYROdev, would it still come up?

No, it wouldn’t. Your description: “just to find my name,”. Anyways, what you can do is grab the players in the game and add their name to an table when the text is changed and check if the first 1-4 characters of their text matches any name inside that table.

Demonstration:

local players = {}
local diff = 0
local input 

script.Parent:GetPropertyChangedSignal("Text"):Connect(function() 
	for _, player in ipairs(game.Players:GetPlayers()) do
		players[player.Name] = player.Name
	end

	for _, name in pairs(players) do
		input = script.Parent.Text:sub(1, 5)
	
		if (input == name:sub(1, 5) ) then
			script.Parent.Text = name
			break
		end
	end
end)
2 Likes

Error rectified, thanks for telling me.

It doesn’t seem to be working, no errors, it just doesn’t match the text.

local players = {} -- Create a table

script.Parent:GetPropertyChangedSignal("Text"):Connect(function() -- start when the text is changed
	for _, player in ipairs(game.Players:GetPlayers()) do -- get a list of all players
		players[player.Name] = player.Name -- absolutely no clue
	end
	
	for _, name in pairs(players) do
		if (script.Parent.Text:lower():sub(1, 4) == name:sub(1, 4)) then -- if the text upto 4 characters matches then
			script.Parent.Text = name -- change the text
			break
		end
	end
end)

No script errors either.

Wait, I changed ipairs to pairs and it worked.

1 Like

seems like it does actually work, it’s just ipairs changed to pairs and it decided to work

Thanks for your help!

Note that the code I provided isn’t efficient but just a demonstration to get you started.

2 Likes

Ah, gotcha!

I shall study this further. You’ve been a big help, because I’m still learning tables. Really appreciate it.