Auto-completing player usernames in text boxes?

When typing into a Text Box, how do I make it auto-complete a player’s username that is in the server?

Any kind of information is appreciated!!

The title shouldn’t say it all. Simplify it to something like “Player Name Auto-Complete” and provide more details in the body of your thread.

1 Like

I’d suggest utilizing string.match which can be used to help identify a player’s name based on a partial string, ex.

TextBox Input:
twentych

Automatch:
TwentyCharacterUsername

However you should be warned that this will only return the first result it finds if any, so if there are multiple user names that contain twentych, it will only return the first it finds.

Here’s the article link; (Just scroll till you find string.match)

2 Likes

I disagree, I think OP should use string.sub. String.sub takes two numbers as arguments, which are the position of the character in a string you want to start with and end with. Tricky to explain so here are some examples:

print(string.sub("Nicolas_Caged", 1, 4)) 
>Nico

print(string.sub("Nicolas_Caged", 4, 6)) 
>ol

You should test a username to see if its compatible by doing this

local userInput = "Nicolas_Ca"
local userName = "Nicolas_Caged"

if string.sub(userName, 1, #userInput) == userInput then 
   return true
end

For clarities sake, the #operator returns the length of the string

3 Likes

An added benefit of using a string.sub check is that you can loop through all names and determine if there are more than 1 results still. As long as more than 1 results are returned it shouldn’t autocomplete (or should display a list of all matching options to pick between).