Auto Username Completer

I have seen some admin systems where you can just type in a part of a username and have the system guess the rest of the username. How are you able to do this?

3 Likes

The script most likely uses a string function such as find or sub.

Read more about it here
http://lua-users.org/wiki/StringLibraryTutorial

I was searching around, and found a solution to your problem, known as fuzzy string searching:

Although this was a solution to a search engine for pets’ names, it can be easily implemented for players’ usernames.

4 Likes

To just have the first parts of the name match up, (like “Name” matching “Name1234”) you could just sub both names and compare them. Fuzzy string searching takes a lot more effort than necessary.

One way to check for which player the part of the username matches:

local match = nil -- the player who it matches with

for _, player in pairs(game:GetService("Players"):GetPlayers()) do -- for each player,
     if guess == player.Name:sub(1, #guess) then -- if the guess matches the part of the player's name that is that long
          match = player
          break
     end
end
1 Like

This maybe will help you

function FindShortName(TargetName)
local Found = false

for i, v in next, game.Players:GetPlayers() do
if string.find(v.Name, TargetName) then Found = v
break
end
end

return Found

FindShortName("Target") -- enter player name in **Target**
1 Like
local Players = game:GetService("Players")

function FindPlayer(String)
	local len = #String
	
	for i,v in next,Players:GetChildren() do
		if (string.lower(string.sub(v.Name,1,len)) == string.lower(String)) then
			return v
		end
	end
	
	return false
end
1 Like

I use this method:

local function findPlayer(arg)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Name:lower():sub(1, #arg) == arg:lower() then
			return v
		end
	end
end
3 Likes

To add to @goldenstein64’s reply, I wrote a fuzzy search function for you to use.

Edit: Just realized that the previous script I posted wasn’t very user friendly so I updated

local function FuzzySearch(ComparisonStrings, SearchString)
	local SearchResults = {}
	
	for _,ComparisonString in pairs(ComparisonStrings) do
		local ComparisonStringLength = #ComparisonString
		local SearchStringLength = #SearchString
		
		if SearchStringLength <= ComparisonStringLength + 5 then
			
			local TempComparisonString = ComparisonString:lower()
			local TempSearchString = SearchString:lower()
			
			local Cost
			
			local Rows = ComparisonStringLength  + 1
			local Columns = SearchStringLength + 1
			
			local Distance = {}
		
			for i = 1, Rows do
				Distance[i] = {}
				
				for k = 1, Columns do
					Distance[i][k] = 0
				end
			end
			
			for i = 2, Rows do
				for k = 2, Columns do
					Distance[i][1] = i
					Distance[1][k] = k
				end
			end
			
			for i = 2, Columns do
				for k = 2, Rows do
					if TempComparisonString:sub(k - 1, k - 1) == TempSearchString:sub(i - 1, i - 1) then
						Cost = 0
					else
						Cost = 2
					end
					
					Distance[k][i] = math.min(
						Distance[k - 1][i] + 1,
						Distance[k][i - 1] + 1,
						Distance[k - 1][i - 1] + Cost
					)
				end
			end
			
			table.insert(SearchResults, 
				{
					Ratio = ((ComparisonStringLength + SearchStringLength) - Distance[Rows][Columns]) / (ComparisonStringLength + SearchStringLength),
					Word = ComparisonString
				}
			)
		else
			table.insert(SearchResults,
				{
					Ratio = 0,
					Word = ComparisonString
				}
			)
		end
	end
	
	table.sort(SearchResults, function(A, B)
		return A.Ratio > B.Ratio
	end)
	
	return SearchResults -- Returns in the format of { { Ratio, Word }, { Ratio, Word } }. A ratio of 1 means it completely matches and a ratio of 0 means nothing matches at all.
end

local TestStrings = {"Wooden Arrow", "Iron Arrow", "Steel Arrow", "Iron Sword", "Iron Boots", "Iron Pickaxe"}
local SearchString = "Arrow"

local Results = FuzzySearch(TestStrings, SearchString) -- Returns most relevant results first

for _,Result in pairs(Results) do -- Prints "Iron Arrow, Steel Arrow, Wooden Arrow, Iron Sword, Iron Boots, Iron Pickaxe"
	print(Result.Word, Result.Ratio)
end
4 Likes